I am trying to link to action addData in the entries controller. I have constructed the link like this:
<%= link_to image_tag (w.link, :border =>0) ,:controller => :entries, :action => :addData %>
but when I click on the link, I get this error:
Couldn't find Entry with ID=addData
I'm pretty sure this is because I have a restful design. Is there a way around this problem? Thanks for reading.
Rails has migrated wholly to a RESTful design. This means that in order to use non standard actions you have to add them to your resources in config/routes.rb.
If they operate on all resources you add them to the hash :collection => {:addData => :post}
In case you have one operating on a single resource with an id use :member.
To some it up for you.
map.resources :entries, :collection => {:addData => :post}
To use the old style of mapping any action to any controller you can add the following two lines to your config/routes.rb
map.connect ':controller/:action/:id.:format'
map.connect ':controller/:action/:id'
Have you defined the route properly for this action addData?
By the way try this :
<%= link_to image_tag (w.link, :border =>0) ,{:controller => :entries, :action => :addData} %>
Related
I need a bit of help with converting routing from Rails 2 to Rails 3.
In app/views/layouts/application.html.erb, I have:
<%= link_to "Reports", reports_path %><br>
There is a ReportsController, and in app/views/reports/index.html.erb, I have this:
<%= link_to "Clients With Animals", :action => "getAnimals", :controller => "clients" %>
Then, in config/routes.rb, I have this (Rails 3)
match '/reports' => "reports#index"
match '/clients/getAnimals', to: "clients#getAnimals"
I get this error when I click on the "getAnimals" link on the reports page:
ActiveRecord::RecordNotFound in ClientsController#show
Couldn't find Client with id=getAnimals
I don't want "getAnimals" to be the ID - I want it to be the action, instead.
How do I do that?
Assuming you also have a resources :clients entry, you want to make sure match '/clients/getAnimals', to: "clients#getAnimals" is above it (Rails will match whatever it hits first).
However, the better way may be to put it in the resource:
resources :clients do
get 'getAnimals', :on => :collection
end
In RoR 3, I just want to have a link/button that activates some action/method in the controller. Specifically, if I click on a 'update_specs' link on a page, it should go to 'update_specs' method in my products controller. I've found suggestions to do this on this site:
link_to "Update Specs", :controller => :products, :action => :update_specs
However, I get the following routing error when I click on this link:
Routing Error No route matches {:action=>"update_specs",
:controller=>"products"}
I've read up on routing but I don't understand why I should have to route this method if all other methods are accessible via resources:products.
You need to create a route for it.
For instance:
resources :products do
put :update_specs, :on => :collection
end
Also by default link_to will look for a GET method in your routes. If you want to handle a POST or PUT method you need to specify it by adding {:method => :post } or {:method => :put } as a parameter, like:
link_to "Update Specs", {:controller => :products, :action => :update_specs}, {:method => :put }
Or you can use button_to instead of link_to which handles the POST method by default.
I was trying to accomplish the following:
<%= link_to "Log out", { :controller
=> 'users', :action => 'logout' }, :class => 'menulink2' %>
But it didn't work, it always redirected me to a show view. I had to had the following to my routes.rb:
map.connect 'users/logout',
:controller => 'users', :action =>
'logout'
Why didn't rails recognize the action I was passing ('logout') ?
That logic has to be specified somewhere. There's got to be some mapping from the hash {:controller => 'users', :action => 'logout'} to a url, and the place that's done in rails is the routes.rb file. In older versions of rails many routes.rb came with a default at the end:
map.connect ':controller(/:action/(:id(.:format)))'
Which would make it so that most any :controller, :action hash could be specified and then routed to host.url/:controller/:action.
With the more modern versions resource-based routes are heavily favored, and controllers which don't follow rails' REST conventions (i.e. having only :index,:show,:create,:new,:edit,:update,:destroy methods) generally have to have their routes explicitly specified in some way.
(Either with map.resources :users, :collection => {:get => :logout} or with map.connect( 'some_url', :controller => 'users', :action => 'logout'}))
I'm guessing, but the reason they did that is probably that the actions of a controller are really just its public methods.
It's frequently nice to have public methods in your controllers that aren't url-end-points for testing purposes.
For instance, you could have before_filters as public methods that you might want to test without having to use #controller.send(:your_before_filter_method) in your test code.
So they whitelist the resource actions, and make the others unreachable by default. Let me look through the rails changelog and see if I'm right.
I have two routes:
map.all_devices '/all_devices', :controller => 'all_devices', :action => 'initialize_devices'
map.show_user_date_select '/all_devices', :controller => 'all_devices', :action => 'show_user_date_select'
I want a user to click on a button, do the show_user_date_select action then be redirect back to mysite.com/all_devices. The route above does work, but always sends the user to initialize_devices, which resets the 'show_user_date_select' action.
Looks like you mapped both of those to the same route. Since you put the initialize_devices one on top, it renders that one with higher priority, which is why you always get that.
Probably what you want is something like this in the routing:
map.all_devices '/all_devices', :controller => 'all_devices', :action => 'index'
Then have a different route which the form submits to, such as /all_devices/show_user_date_select, and redirect to /all_devices afterwards:
def show_user_date_select
# do stuff here
redirect_to all_devices
end
The routes seem a little odd to me. Try something like:
map.resources :all_devices, :member => { :all_devices => :get, :show_user_date_select => :get }
Then in your views:
<%= link_to "All Devices", path_to_all_devices_all_devices %>
<%= link_to "Show Dates", path_to_show_user_date_select_all_devices %>
The link names are awful, but should work with your existing app. I would rather see:
map.resources :devices, :member => { :all => :get, :select => :get }
<%= link_to "All Devices", path_to_all_devices %>
<%= link_to "Show Dates", path_to_select_devices %>
But that will require a bit or re-plumbing on your part.
It looks like that route explicitly maps /all_devices to the initialize_devices action.
Perhaps you should set some piece of session-specific information the first time initialize_devices is reached and does nothing on subsequent requests.
Im using a link which is
:communities,:action=>:usergroups} , :class => "adminbutton viewusergrp" %>
and routes contain the
map.resources :vccommunities,:member => {:usergroups => :get}
and some more action names are also specified here in member and collection.
Im taking care of alphabetical order while listing these actions within :member
But while clicking on that link ...it is taking :action => show, :id => usergroups,
:controller => :communities
so it is thowing error ,since it is not going to the proper action in the controller.
Please help me in that.
the problem is not with your route problem is with your link. as per the defination of routes rails internally searches for id in link hash in your eg link like this
<%= link_to 'View User Group', {:controller=>:communities,:action=>:usergroups, :id => 20} , :class => "adminbutton viewusergrp" %>
should work cause this will map to
/communities/20/usergroups
for further refrence can be taken from this page