I’m pretty new to rails and I’ve created a method in
posts_controller.rb that contains the following
def update_feeds
Post.get_feeds -- works via console
@rss_feedsTab = "/admin/posts"
redirect_to @rss_feedsTab, :notice => 'Feeds Updated successfully'
end
and trying to make it fire in my view with:
<%= link_to 'Update Feeds', :controller => "posts", :action => "update_feeds", :method=>:post %>
and I get a routing error:
No route matches {:action=>"update_feeds", :method=>:post, :controller=>"admin/posts"}
I’m really not grasping how this whole routing works at all, any help would be appreciated 🙂
CONTROLLER=posts rake routes:
admin_post GET /admin/posts/:id(.:format) admin/posts#show
PUT /admin/posts/:id(.:format) admin/posts#update
DELETE /admin/posts/:id(.:format) admin/posts#destroy
GET /admin/posts(.:format) admin/posts#index {:collection=>{:update_feeds=>:post}}
POST /admin/posts(.:format) admin/posts#create {:collection=>{:update_feeds=>:post}}
GET /admin/posts/new(.:format) admin/posts#new {:collection=>{:update_feeds=>:post}}
GET /admin/posts/:id/edit(.:format) admin/posts#edit {:collection=>{:update_feeds=>:post}}
GET /admin/posts/:id(.:format) admin/posts#show {:collection=>{:update_feeds=>:post}}
PUT /admin/posts/:id(.:format) admin/posts#update {:collection=>{:update_feeds=>:post}}
DELETE /admin/posts/:id(.:format) admin/posts#destroy {:collection=>{:update_feeds=>:post}}
routes.rb
namespace :admin do
resources :users,:videos,:posts,:links,:rss_feeds
resources :posts, :collection => {:update_feeds => :post}
end
I think you would do well to pay attention to two things here:
1) The method’s name in the controller must match the view’s name. That is one of the conventions of Rails. For instance, an
indexmethod inposts_controller.rb(posts#indexfor short) will automatically look for a view calledposts/index.html.erb.2) Also, your routes establish which names you should use for your controller methods (also called controller actions). Those names are based upon the HTTP verbs (index, new, create, edit, update, show, destroy). So, controller actions are also not named randomly. I would suggest that you work from one of the existing actions listed in your routes.
Rails does allow you to easily create your own actions with the names you want, but you should probably play by the conventions first, and only then try to begin customizing things.
That particular error message you’re getting means there is no route associated with the controller action you just created. As I said, creating controller actions involves creating associated views and routes — and that’s why I discourage you to do so at this point. Start from the routes, look at which ones you have and go from there.