LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-21-2011, 07:36 AM   #1
windstrewn
LQ Newbie
 
Registered: Oct 2007
Location: Switzerland
Distribution: Debian, Ubuntu
Posts: 10

Rep: Reputation: 0
defining own methods in Rails 3 controllers and creating appropriate routes


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!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] [Ruby on Rails] backticks don't work under rails. bartonski Programming 1 09-08-2009 04:19 PM
Defining loop statement for creating new file each time neo009 Linux - Newbie 6 04-12-2009 06:49 AM
Static routes using routes/ifroutes-eth-id.. files and default gateway disabled TimtheEagle Linux - Networking 0 05-31-2008 07:08 PM
LXer: Real world Rails: Caching in Rails LXer Syndicated Linux News 0 05-19-2007 09:16 PM
ICMP Redirect creating cached routes drheams Linux - Networking 0 07-22-2003 10:53 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration