Problems with "link_to"
NEW EDITING
With resources :cases
in the routes.rb
all these routing helpers work as expected (c is an instance of the model Case):
cases_path # Index action
new_case_path # New action
case_path(c) # Show action
edit_case_path(c) # Edit action
Changing routes.rb
to this:
scope "/:area" do
resources :cases
end
two routing helpers fail (see error below):
cases_path # OK
new_case_path # OK
case_path(c) # Fail
edit_case_path(c) # Fail
To make it works I need to change the last two helpers in:
cases_path # OK
new_case_path # OK
case_path(c, :area => params[:area]) # OK
edit_case_path(c, :area => params[:area]) # OK
Why there is no need to give :area to the first two helpers and there is a need for the second two? It makes the code confusing...
OLD QUESTION
Initially I had this routes.rb:
resources :cases
and I could generate links of this type
exa开发者_Python百科mple.com/cases/3
to show the case with ID = 3 with:
link_to("Show this case", @case)
Then I modified routes.rb to:
scope "/:area" do
resources :cases
end
end the link_to above give me the error
No route matches {:action=>"show", :controller=>"cases", :area=>#<Case id: 2,
It seems that rails does not get the :area from the parameters automatically. I could fix this forcing the area creating a link with
link_to("Show this case", case_path(params[:area], @item))
to obtain the link to
example.com/area1/cases/3
but I really don't like it. Am I doing something wrong? Why rails bring over the controller and the action but forget about the area?
Try this
link_to "Show this case", [:area, @case]
EDIT (remove the colon)
scope "/area" do
resources :cases
end
EDIT 2
link_to "Show this case", case_path(@case, :area => :en)
If you specified area
as argument you should always pass it.
Add this method to your ApplicationController:
def default_url_options(options={})
{:area => params[:area]}
end
You then don't have to manually set the :area option in your routing helpers
精彩评论