basic routing - how to create a link_to path from match - ruby-on-rails

Ok, say you have this:
match "tutor_appointments/new_appt" => "tutor_appointments#new_appt"
How do I create a link_to path from it?
Would it be something like this: (it doesn't work, btw)
<%= link_to "New Appointments", controller:tutor_appointments, method:new_appt %>
I'm always confused on routing stuff when it comes to figuring out how to do link_to link.
I do understand that tutor_appointments is the controller and new_appt is the method.

Ideally, you would name the route:
http://guides.rubyonrails.org/routing.html#naming-routes
And then you can refer to the route by that name.
eg. if you had:
match "tutor_appointments/new_appt" => "tutor_appointments#new_appt", as: 'new_appointment'
Then you could do:
link_to 'New Appointments', new_appointment_path
However, in this case it sounds like what you actually want is resource routing:
http://guides.rubyonrails.org/routing.html#resources-on-the-web
And you want a 'new' action for your 'tutor_appointments' resource.

Related

How to get the route by helper name?

I have the following set of routes which point to the same view:
get 'mypath', to: 'home#mypath', as: 'mypath'
get 'mypath-v2', to: 'home#mypath', as: 'mypath_v2'
get 'mypath-v3', to: 'home#mypath', as: 'mypath_v3'
How can I check if I am using one route or the other inside the view?
For example if I want to get mypath-v2 or mypath_v2, how would I do it?
Well, as for me it is better to do such things using params. You can define your routes like this:
get "mypath/:version", :as => "mypath"
In this case you will be able to use params[:version] to clarify current path.
You would call it by appending _path or _url
For instance here is an example in a link:
<%= link_to 'Link to mypath-v2 page', mypath_v2_path %>
OR
<%= link_to 'Link to mypath-v2 page', mypath_v2_url %>
To compare them you can look at the request object. When you call request.path or request.fullpath it will bring in the actual address request path of the link.
So...
<%= if request.path.eql?('mypath-v2') ? "Used It" : "Other Route" %>

Action Controller error. Url Generation error. No route matches

I'm working through the "Ruby on rails 3 essential training" on lynda.com and am having an issue while generating my server. So far I have a subjects_controller.rb, linked to my views folder, to the file list.html.erb. My error when trying to start the server is:
No route matches {:action=>"show", :controller="subjects", :id=>1}
In my list.html.erb file I have written the code:
<td class="actions">
<%= link_to("Show", {:action => 'show', :id => subject.id}, :class => 'action show') %>
<%= link_to("Edit", '#', :class => 'action edit') %>
<%= link_to("Delete", '#', :class => 'action delete') %>
</td>
My subjects_controller.rb looks like:
class SubjectsController < ApplicationController
def list
#subjects = Subject.order("subjects.position ASC")
end
end
I have double checked to make sure I have everything written the same as the instructor but there seems to be a missing link. Any ideas? If I totally cut out the action:
<%= link_to("Show", {:action => 'show', :id => subject.id}, :class => 'action show') %>
Then the server starts up. There must be a problem here but I'm not sure what it is. Also when the instructor inputs link_to on his text editor, the txt turns a different color and mine does not. Same thing with his "#" instance variable. Mine doesn't change color. Not sure if this means anything either. Thanks for any input!
Here is my config/routes.rb file:
Rails.application.routes.draw do
root :to => "demo#index"
get 'demo/index'
get 'demo/hello'
get 'demo/other_hello'
get 'subjects/list'
end
Short version: The error message is telling you exactly what is wrong. You have no route that matches, because while your action is named list, your link specifies :action => 'show'.
Longer version: The second argument to the link_to helper is supposed to tell Rails what URL to generate for the link, usually by specifying one of your routes by name, but sometimes (as in this case), by specifying the action (and optionally the controller). You're specifying the action show. The subjects controller is implied. Therefore, Rails is trying to find a route (in the ones defined in your routes.rb) that GETs the SubjectsController#show action. However, as you can see from your routes.rb, you only define one route on the SubjectsController, and that's list.
If you're ever confused about what routes you have or what their names are, you can use the rake routes task to list them all out in a nice readable format.
Edit to respond to followup question:
The instructor is telling me that when you generate a controller and
action that its supposed to add a route to the routes.rb folder. This
worked for me earlier but when creating these actions that I'm having
trouble with now, it didn't generate anything in the routes.rb folder.
Do you know why that is?
When your instructor says 'generate', they probably mean 'use the rails generate command'. When you use the generator to create a controller and specify the actions in it, the it will also add those actions to the routes file.
If, on the other hand, you write the action into an existing controller (including using the generator for the controller but not specifying actions), or create the controller file yourself, you'll have to update the routes file manually. If you are using the generator and specifying actions and aren't getting updated routes, I'm not sure what's going on.
Personally, I prefer to write my routes by hand anyway - the generator often doesn't get them exactly right.

What Ruby construct does the link_to method in Rails use?

Say I have this route:
match '/somepage' => "home#somepage"
I can then do this on a different page to link to "somepage":
<%= link_to "Some Page", somepage_path %>
I'm new to both Ruby and Rails, and I'm struggling to understand how exactly the "somepage_path" part works. It's not a string, it's not a symbol, is it a method call? If so, where does that method exist? Seems like I'm missing something obvious...
On initialization all paths are generated according to the routes.rb definitions.
If you want to customize the routes names you can use
match '/somepage' => "home#somepage", :as => "foobar"
Later used as
<%= link_to "Some Page", foobar_path %>
Hope that clears some stuff up about custom routing :)
Link magic is all handled by ActionDispatch::URL::UrlFor, see ActionDispatch::URL::UrlFor

How to get a named route from a class in rails?

I have a common partial for index where i want to insert some links.
On this links i would like to use the named_routes, like
link_to "Say hi",things_path(:param1=>:hello, :params2=>:there)
but i dont know if things_path is users_path, places_path or reviews_path, because this partial is shared for all controllers. There is any way to get the named route associated to the class or the current controller.
I want something like this
link_to "Say hi", path_of(#current_class)(:param1=>:hello, :params2=>:there)
There are several approaches to this. The simplest thing is to rely on polymorphic routing, such as: link_to "Say hi", #my_object. In this case rails will look at the class of #my_object and its current state (new_record?) and use the appropriate route and restful action. Assuming your partial is named _foo.html.erb then this can be as simple as
link_to 'Say hi', foo
... which is pretty awesome. This question has been asked before, here: Polymorphic Routes in Rails - in views but I guess it's hard to find answers without knowing the magic words "polymorphic routing" :-)
A path helper like cars_path is ultimately just a shortcut to setting :controller, :action and other params like :id. If you want to make the controller always equal to the current controller you can just say
link_to "Say hi", :action => "index", :param1=>:hello, :params2=>:there
Because the :controller option is omitted, it will be assumed to be the current controller.

Rails: How do I add an additional parameter to resource-based link_to call?

I have a resource defined in routes.rb like so:
map.resources :users
I like using the cleanest link_to option, which would be:
link_to #user
I'd like to add an additional parameter to this call, though: "view=local"
If I were using the user_path, I'd do this:
link_to user_path(#user, { :view => 'local' })
Is there a way to get the same result without including the user_path function explicitly? Ideally, I'd do something like:
link_to #user, { :view => 'local' }
but that doesn't seem to work.
No, you can't. The only real control you have is the ability to add more params on the link tag. link_to has a third parameter that would let you add things like a title attribute on the HTML tag, but it's not going to modify the href value.
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#M001597
Go ahead and use the user_path helper. As with most things in Rails, if you want to do the default action, it's going to be simple. You want to do something more complex, so you have to do more work :)

Resources