开发者

Rails 3 Route: button_to HTML INPUT Action not working

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!


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜