Rails champions
I've been programming with Ruby and Rails 2.x for quite a while and while I'm certainly not proficient in Rails, I'd still say that I had a nice grasp of the framework and the tools. However, I am currently working on my first Rails 3 project (currently using 3.0.5) and I am growing increasingly desperate with the new Rails 3 routing interface.
Of course, I have been following Ryan Bates' RailsCasts (http://railscasts.com) in general and on the topic in particular.
Of course, I have read many, many websites about the topic (e.g. RailsGuides: Rails Routing from the Outside In or The Lowdown on Routes in Rails 3, etc., etc...). I am even one of those old-fashioned guys who reads books, so I have also been following the Beta Book on Rails 3 by The Pragmatic Programmers. And, last but not least, I did try to find an appropriate answer on this site.I guess you could say, that I really tried to find my way round before pestering you with my question. But nothing seems to really cover my problem. Yet, I am pretty sure, that the solution can't be too difficult. I've been trying to find a sol开发者_StackOverflow中文版ution for days now I just don't see what my mistake is.
Let me first give you a quick rundown on what I already can do:
I have no problems with the standard Rails methods predefined by entries in routes.rb, such as:
resources :my_controller
I also managed to get some of my own GET-methods to work, e.g.:
# in routes.rb
resources :some_items do
get :my_additional_method, :on => :member
end
# in a view of SomeItemController:
<%= link_to 'invoke additional method', [:my_additional_method, @some_item], :class => "some_css_class" %>
HOWEVER, the follwing situation is giving me real headaches:
# in ItemController
def my_method
raise params.to_yaml
end
# in routes.rb
resources :items do
post :my_method, :on => :member
end
# e.g. in edit view of ItemController:
<% form_tag ( my_method_item_path(@item), {:class => :some_css_class} ) do %>
...
<% end %>
# 'rake routes' output excerpt
my_method_item POST /items/:id/my_method(.:format) {:controller =>"items", :action=>"my_method"}
# ERROR during the rendering of the view
No route matches {:action=>"my_method", :controller=>"items", :id=>#<Item id: ...>}
I tried dozens of variantions of the above. For example, the following variation doesn't help:
# variation of form_tag in view:
<% form_tag ( {:controller => "items", :action => "my_method", :id => @item }, {:class => :some_css_class} ) do %>
The only thing I got working - which, however, is NOT what I want - is the following:
# in ItemController
def my_method
raise params.to_yaml
end
# in routes.rb
resources :items do
post :my_method, :on => :collection
end
# e.g. in edit view of ItemController:
<% form_tag ( my_method_items_path(@item), {:class => :some_css_class} ) do %>
...
<% end %>
# 'rake routes' output excerpt
my_method_items POST /items/my_method(.:format) {:controller =>"items", :action=>"my_method"}
As you probably have guessed already, I lose the "id" param when invoking the method on the collection instead of the member. Please note, that I am aware of how important the "s" at the end of the "item" is. I know you need to leave it out in certain situations and to add it in others.
Can anyone help me out here? What is my mistake?
Thanks a lot for your help!
<% form_tag (my_method_item_path(@item), :class => :some_css_class, :method => :post ) do %>
...
<% end %>
Got to this URL to learn about adding More RESTful Actions to a "resources"d controller:
http://guides.rubyonrails.org/routing.html#adding-more-restful-actions
resources :photos do
member do
get 'preview'
end
end
To add the following example method route:
/photos/1/preview
This will call the preview method of the photos controller with the ID 1
精彩评论