ruby on rails adding new route - ruby-on-rails

i have an RoR application Log, which similar to the book store app, my logs_controller has all default action: index, show, update, create, delete..
now i need to add new action :toCSV, i defined it in logs_controller, and add new route in the config/routes as:
map.resources :logs, :collection => { :toCSV => :get }.
from irb, i checked the routes and see the new routes added already:
>> rs = ActionController::Routing::Routes
>> puts rs.routes
GET /logs/toCSV(.:format)? {:controller=>"logs", :action=>"toCSV"}
then ran ‘rake routes’ command in shell, it returned:
toCSV_logs GET /logs/toCSV(.:format) {:controller=>"logs", :action=>"toCSV"}
everything seems working. finally in my views code, i added the following:
link_to 'Export to CSV', toCSV_logs_path
when access it in the brower 'http://localhost:3000/logs/toCSV', it complained:
Couldn't find Log with ID=toCSV
i checked in script/server, and saw this one:
ActiveRecord::RecordNotFound (Couldn't find Log with ID=toCSV):
app/controllers/logs_controller.rb:290:in `show'
seems when i click that link, it direct it to the action 'show' instead of 'toCSV', thus it took 'toCSV' as an id...anyone know why would this happen? and to fix it? Thanks...

map.resources :logs, :collection => { :toCSV => :get }
I think this is perfect. you must restart your server evry time you change the config/routes.rb
It's no answer though but it's important.

This can be a workaround:
Create a named resource:
map.toCSV 'logs\toCSV', :controller => :logs, :action => :toCSV
I am really sorry i forgot to mention the main point!
In your view it should be:
link_to 'Export to CSV', toCSV_path
Also, these named routes come in handy especially when you have authentication involved. For instance, during signup, rather than directing the user to \user\new you can direct him to \signup. Its more friendly.
Thats it!!
Its simpler and it works. Cheers! :)

Remove the map.resources line from routes.rb, and then run rake routes. If you see a route /logs/:id, that is the route that should probably be removed.

Related

Getting error while using RefineryCMS Blog route

I instructed config/routes.rb to use Refinery Blog as a root directory:
root :to => "refinery/blog/posts#index"
mount Refinery::Core::Engine, :at => '/'
In a app/view/layouts/_header.html.slim I'm trying to use blog_root route. For example:
= link_to (image_tag "/logo.gif"), blog_root, class: "brand"
The route is listed when I issue rake routes:
blog_root /blog(.:format) refinery/blog/posts#index
But nothing shows up, the system gives an error:
undefined local variable or method `blog_root' for
#<#<Class:0x00000005e62f80>:0x007fd7241d94c8>
Also, I tryed blog_root_path, but it didn't work either.
Anything I can do in this situation? Thanks a lot!
This question/answer pair was helpful.
I looked inside config/routes.rb of main app, and in comments it was writted that 'We ask that you don't use the :as option here, as Refinery relies on it being the default of "refinery"'.
So, the working route is refinery.blog_root_path.

Rails routes.rb unexpected behaviour

Editing my Rails 4 app routes.rb file and I'm getting unexpected behaviour (unexpected form my point of view anyway).
I'm trying to create a link that updates a booking record. I have created an action in my BookingsController called WITHDRAW ready to handle the update process. I would like the link to pass the booking id and my code for the link is this:
<%= link_to "Withdraw this booking", bookings_withdraw_path(#booking), :confirm => "Are you sure you want to withdraw this booking?", :method => :patch %>
My problem arises when I try and setup the route for this link. If I add the following line to my routes file:
match 'bookings/withdraw/:bid' => 'bookings#withdraw', via: 'patch'
then when I run the rake command to check the routes it shows this:
bookings_withdrawn GET /bookings/withdrawn(.:format) bookings#withdrawn
PATCH /bookings/withdraw/:bid(.:format) bookings#withdraw
As you can see, the WITHDRAW path is part of the one above (WITHDRAWN is a different path by the way). If I remove the /:bid part from the path then it creates it's own path which is what I would expect.
Can someone explain why this is happening?
try this out
in you routes file pass a block to resources :bookings like this
resources :bookings do
member do
patch :withdraw
end
end
and remove this
match 'bookings/withdraw/:bid' => 'bookings#withdraw', via: 'patch'
As I've written in comment, you should add :as option to your route, i.e.:
match 'bookings/withdraw/:bid' => 'bookings#withdraw', via: 'patch', as: 'bookings_withdraw'
Named route probably wasn't autogenerated because of dynamic part :bid, AFAIK Rails aren't generating implicitly named routes in such cases, so you have to add them explicitly, but I still can't find it in an docs, maybe if somebody've got and can share so I'll update my answer.

Set Up Route for Accessing Private S3 Content

I've been following
https://github.com/thoughtbot/paperclip/wiki/Restricting-Access-to-Objects-Stored-on-Amazon-S3
and
Rails 3, paperclip + S3 - Howto Store for an Instance and Protect Access to try and get Paperclip's expiring links to work. I believe most of what I'm running into is one of the routing variety.
In my pieces_controller I put a method in like this
def download
redirect_to #asset.asset.expiring_url(1000)
end
And then in my routes, I put this:
match "pieces/download"
Then in my view I have:
<%= link_to download_asset_path(piece)%>
It would seem to be far from working, and I'm not sure what is messed up. I know I'm getting routing errors for one, but it's also telling me that my download_asset_path is undefined, which is likely also routing related... I feel like I'm doing everything all wrong.
Tearing my hair out. Thanks!
Try modifying your routes file to:
match 'pieces/download' => 'pieces#download', :as => 'download_asset'
Your match needs to tell which controller#action to go to, and the as option will allow you to name the route download_asset_path.
If your pieces controller is for a Piece resource it could be cleaner like:
resources :pieces do
member do
get :download
end
end
But then you would want to change the link to:
link_to 'Link text', download_piece_path(piece)
For further reading: http://guides.rubyonrails.org/routing.html

Help with rails routes

Im having a little trouble setting up routes.
I have a 'users' controller/model/views set up restfully
so users is set up to be a resource in my routes.
I want to change that to be 'usuarios' instead cause the app will be made for spanish speaking region... the reason the user model is in english is cause I was following the authlogic set up and wasnt sure if naming the model usuario instead would create trouble.. so basically this is what I have in mr routes.rb to get this functionality done.
map.resources :usuarios,:controller=>"users", :path_names => {:edit => 'editar' }
the problem is that when I try to register a new user I get this error
ActionController::MethodNotAllowed
Only get, put, and delete requests are allowed.
this happens after I have filled out my register form and clicked on submit...
Have you tried using the 'as' option to change how the url looks without modifying the routes?
This example is from the documentation:
# products_path == '/productos'
map.resources :products, :as => 'productos' do |product|
# product_reviews_path(product) == '/productos/1234/comentarios'
product.resources :product_reviews, :as => 'comentarios'
end
You can try rake routes | grep usuarios from a terminal window (cd to the project root first) to make sure that the proper named routes are setup properly. You can cross reference that with the form tag you are using to make sure that the action for the form is correct.

New Action Not Working

I have a standard User controller with the normal set of actions (index, show, new, edit, etc) and I'm trying to add a new action named 'profile'. I added the following code:
def profile
#user = User.find(session[:user_id])
end
I also created a new view for the action (app/views/users/profile.html.erb), but whenever I try to view that page I get an error:
ActiveRecord::RecordNotFound in UsersController#show
Couldn't find User with ID=profile
...
Apparently it's hitting the show action. I'm guessing that means I need to add something to my routes to make this work, but I don't know what. So far I just have the two default routes and the map.root line which I uncommented:
map.root :controller => "home"
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
So really I have two questions:
What do I have to do in order to enable my new action?
Why don't the existing routes cover this situation? Other urls consisting of just the controller and action work just fine (e.g. http://localhost:3000/users/new). Why not this one? Shouldn't it just evaluate to :controller = users, :action = profile, :id = nil?
Try putting something like this in your routes.rb file:
map.user_profile '/users/:id/profile', :controller => "users", :action => 'profile', :conditions => {:method => :get}
I think possibly the reason it's doing this is because you are not matching either of the defaults, because you are not setting :id (even though it is detecting your action as the id). I don't know what your URL looks like, but I have a feeling that if you tried http://localhost:3000/users/123124124/profile, it MIGHT work, even without the new line in routes.
Are you intentionally trying to get the id from session[:user_id] instead of params[:id]? Is this supposed to be displaying a public profile?
Are you sure, that the session contains the user_id the first time you load the page?
Hard to say without seeing all the code, but my guess is there may be some strangeness because your model and controller have the same name. I'd try renaming the controller before changing anything else (remember to change the name of the views/users directory too).
See also this other stack overflow post: Rails cannot find model with same name as Ruby class
Long shot maybe.
Solution for your problem configure your routes.rb in such a way that id should be passed as the parameter .
configure in routes.rb as below
map.profile '/profile/:id',:controller=>'users',:action=>'profile'
when you want to access your profile page use it with the following URL
http://localhost:3000/profile
Make sure once you login , u handle the session and store the userid in the session variable .
Good luck !

Resources