In my project, stories belong to users and typically all story access is through nested resourcing, with the exception of an action to show a global list of all stories. In my routes.rb file I have:
resources :users do
resources :stories
end
match "/all_stories" => "stories#all_stories"
the match
statement generates the following route:
all_stories /all_stories(.:format) {:controller=>"stories", :action=>"all_stories"}
In my stories_controller.rb
file I have an action called: all_stories
.
def all_stories
@stories = Story.all
end
From my view I'm calling link_to
in this manner:
<%= link_to "All Stories", all_stories_path %>
resulting in this error:
No route matches {:action=>"show", :controller=>"stories", :user_id=>nil,
:id=>#<Story id: 1, title: "test story 01", desc: "Edited desc for test story 01",
activity: 20, created_at: "2011-11-28 01:07:08", updated_at: "2011-11-28 01:26:31",
user_id: 5>}
In my stories_controller_test.rb
the following GET calls for :all_stories
generate the listed errors:
get( 'all_stories' )
Undefined method `story_path' for #<#<Class:0x0000010430da18>:0x00000101789d10>
get( :all_stories )
undefined method `story_path' for #<#<Class:0x00000102a178b0>:0x00000100ee9cc8>
get( '/all_stories' )
No route matches {:controller=>"stories", :action=>"/all_stories"}
get( {:action=>"all_stories"} )
No route matches {:controller=>"stories", :action=>"{:action=>\"all_stories\"}"}
On the first two, there is no 'story_path" because stories are nested under users. Not sure what is happening on the next two. What syntax can I use to get
the all_stories
action in my controller?
Try changing the route to
match "/all_stories" => "stories#all_stories", :as => :all_stories
Then in your code, you can reference the all_stories_path
or the all_stories_url