Rails 3 路线:button_to HTML INPUT 操作不起作用

发布于 2024-12-08 04:30:11 字数 1812 浏览 1 评论 0原文

我有一个奇怪的问题,我认为与路线有关。

在我的“视图”中,我有这样的:

<%= button_to "New Item", new_proposal_pitem_path(@proposal), :method => :get %>

我想单击“新项目”按钮,并为提案创建一个新的皮项目。这会生成我期望的 HTML:

<form method="get" action="/proposals/1234/pitems/new" ...><input ...></form>

然而,真正发生的是,当我单击按钮时,它会尝试对 /proposals/1234 而不是 /proposals/1234/pitems/new 进行 GET。这给了我一个“显示”页面而不是“新”页面。有趣的是,我可以手动将 {site}/proposals/1234/pitems/new 直接放入网络浏览器 HTTP 地址中并获得我想要的内容(“新”页面)。但是,如果我将其保留为自己响应按钮单击,则 Rails 会首先将其转换为 /proposals/1234 。

为了让这个更神秘,我在同一个表单上有一个类似的项目,它看起来完全相同:

<%= button_to "New Payment", new_proposal_payment_path(@proposal), :method => :get %>

它生成与另一种情况相同的 HTML:

<form method="get" action="/proposals/1234/payments/new" ...><input ...></form>

但是这个有效!当我单击按钮时,就像我所期望的那样,我会直接转到 /proposals/1234/ payments/new 。我只是不明白是什么让这些行为不同。

我的完整路线文件如下所示:

TCoB::Application.routes.draw do
  resources :proposals do
    resources :pitems, :payments
    get 'list', :on => :collection
  end

  resources :pitems do
    get 'list', :on => :collection
  end

  resources :invoices do
    resources :iitems, :payments
    get 'list', :on => :collection
  end

  resources :iitems do
    get 'list', :on => :collection
  end

  resources :payments do
    get 'list', :on => :collection
  end

  resources :ids

  resources :clients do
    resources :proposals, :invoices
    # Route GET /cients/list
    get 'list', :on => :collection
    get 'list_proposals', :on => :collection
    get 'list_invoices', :on => :collection
  end

  get "home/index"

  root :to => "home#index"
end

有人可以阐明这个问题吗?

谢谢!

I have a strange problem that I think has to do with routes.

In my "view" I have this:

<%= button_to "New Item", new_proposal_pitem_path(@proposal), :method => :get %>

I want to click the "New Item" button, and create a new pitem for a proposal. This generates the HTML I would expect:

<form method="get" action="/proposals/1234/pitems/new" ...><input ...></form>

However, what really happens is, when I click on the button it attempts a GET on /proposals/1234 rather than /proposals/1234/pitems/new. This gives me a "show" page rather than a "new" page. Interestingly, I can manually put the {site}/proposals/1234/pitems/new directly into the web browser HTTP address and get what I want (the "new" page). But rails is, on its own, translating it first to /proposals/1234 if I leave it to its own in response to the button click.

To make this more mysterious, I have a similar item on the same form which looks exactly the same way:

<%= button_to "New Payment", new_proposal_payment_path(@proposal), :method => :get %>

which generates the same HTML as the other case:

<form method="get" action="/proposals/1234/payments/new" ...><input ...></form>

But this one works! It takes me right to /proposals/1234/payments/new when I click the button, just like I'd expect. I just don't understand what makes these behave differently.

My full routes file looks like this:

TCoB::Application.routes.draw do
  resources :proposals do
    resources :pitems, :payments
    get 'list', :on => :collection
  end

  resources :pitems do
    get 'list', :on => :collection
  end

  resources :invoices do
    resources :iitems, :payments
    get 'list', :on => :collection
  end

  resources :iitems do
    get 'list', :on => :collection
  end

  resources :payments do
    get 'list', :on => :collection
  end

  resources :ids

  resources :clients do
    resources :proposals, :invoices
    # Route GET /cients/list
    get 'list', :on => :collection
    get 'list_proposals', :on => :collection
    get 'list_invoices', :on => :collection
  end

  get "home/index"

  root :to => "home#index"
end

Can someone shed light on this issue?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〆凄凉。 2024-12-15 04:30:11

使用 JavaScript 的小辅助方法:

def button_link_to(name, url)
  "<button type=\"button\" onclick=\"window.location.href='#{url}';\">#{h(name)}</button>".html_safe
end


button_link_to "New Item", new_proposal_pitem_path(@proposal)

Small helper method that uses JavaScript:

def button_link_to(name, url)
  "<button type=\"button\" onclick=\"window.location.href='#{url}';\">#{h(name)}</button>".html_safe
end


button_link_to "New Item", new_proposal_pitem_path(@proposal)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文