Hi there, Rails buffs
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...
Had...
I am currently working on my first Rails 3 project (currently using 3.0.5) and I am growing increasingly desperate with the new routing interface
.
Of course, I have been following Ryan Bates'
RailsCasts 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.
I guess you could say, that I really tried to find my way round before pestering you with my question. But the examples in the sources above are all quite alike and don't seem to cover my problem.
Actually, you should think that what I want to do, should be easy, but I've just not been able to implement it for several days now.
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:
Code:
resources :my_controller
I also managed to get non-standard GET-methods to work, e.g.:
Code:
# in routes.rb
resources :some_items do
get :my_additional_method, :on => :member
end
# in some 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:
Code:
# 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.
The following
doesn't help either, for example:
Code:
# 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:
Code:
# 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 out there help me?
What is my mistake?
In Rails 2 I didn't have any problems defining my own controller methods and invoking them.
Am I missing something basic here or is it just a small thing I am doing wrong?
Thanks a lot for your help!