form_for is giving me the wrong url for path_prefixes
I am having a problem with the form_for method in Rails. It is behaving strangely.
I have a route with a path prefix,开发者_运维百科 something like:
map.resources :beers, :path_prefix => '/:brewery'
And I have a form like this (@beer.brewery is a string, just the name of the brewery):
<% form_for @beer, :url => { :brewery => @beer.brewery } do |form|
--some fields
<% end %>
It will set the action of the form to this for a new record.
/brewery_name/beers/new
and this for an existing record.
/brewery_name/beers/1/edit
Anyone knows why this happens or how to fix it?
--edit--
Right now I am solving this like this (for a new record):
<% form_for @beer, :url => beers_path(@beer.brewery) do |form| %>
and (for an edited record)
<% form_for @beer, :url => beer_path(@beer.brewery, @beer) do |form| %>
But I want to do it the same way for new and edit, if it is possible.
Cheers, Thijs.
I'm guessing you want this to go to a brewery's beer. In that case:
<% form_for [@beer.brewery, @beer] do |f| %>
-- some fields
<% end %>
By providing an Array as the first argument to form_for
it will generate a nested resource.
精彩评论