I had a doubt when i was writing rails code.
In my link_to i used my route order to show my order. So:
<% #orders.each do |order| %>
<tr>
<th><%= order.name %></th>
<th><%= link_to 'Mostra', order %></th>
</tr>
<% end %>
I saw my rake routes and there was a :
order GET /orders/:id(.:format) orders#show
If i remember right i generated Order resource with scaffolding. However , when i created by hand new resources (not using scaffolding)
i had a different route for my resource. For example , i have something like name_resource_show(:id) for the show. This kind of style is good cause i understand that i have to pass the id , if i want to see a specific resource. But in the case before , the case of order , i really don't know how rails is able to understand to use the id of the object order. And also:
why i have different routes name? why i have sometimes _path and sometimes (maybe when i generate resource with scaffolding) other things?
i would expect something like order_show(:id) and not simply order.
how it works?
Rails helpers are smart enough to use model object to form url.
<%= link_to 'Mostra', order %> equivalent to <%= link_to 'Mostra', order_path(order) %> and both points to order show page.
This will generate 7 routes for your controller orders.
resources :orders
order GET /orders/:id orders#show
Here order is the helper method it provides to call routes instead of using /orders/:id.
Simply you can use order_path(order) to get route /orders/:id
Similary we get helper for all 7 routes. You can also override the helpers.
Go to below link for more information.
Reference: http://guides.rubyonrails.org/routing.html
First, I recommend following the Rails conventions on routes (see the main reference article here).
Here are answers to your questions in order.
The route you got from rake routes makes sense in the following way. Look at the URL (orders/:id). Within all of your orders, the :id passed specifies which one to look at. The GET nature of the request indicates you are getting the data on that record, i.e. that it is a SHOW action.
Rails understands where the ID is because of how the routes are structured. If you had order GET /orders/:year/:id in routes, then Rails will know to look for the third parameter for the ID it needs.
The two for accessing routes options are _path and _url (see here for details), but there are some alternatives explained in the main reference article I linked at top.
You can still use the explicit route, but the order option is simply a bit of sugar Rails offers to make things easier to read.
Related
I have a model "affiliate" and using the resources for resources :affiliates
I want to have a page site/affiliate so I added get '/affiliate', to: 'affiliates#affiliate' to routes.rb
When I go to /affiliate it shows a form and when I go to /affiliates it lists the affiliates. But now, every time I call #affiliate it changes the address to /affiliate.id example: When i click on the link show <td><%= link_to 'Show', affiliate %></td> it goes to the singular form .id instead of plural /id /affiliate.5
Is that a way to fix or I will have to change the view /affiliate to something different?
When you use a record in link_to, Rails need to figure out the path from the record. One of the things it does is to call to_param, which gives the id of the record by default. Usually it'll be able to match /affiliates/id. But the matching is not unique, and it can match some other possibilities too (I'm not sure about all the possible matches when a url is extracted from a record). What's important is that Rails will take the first matching route. In your case it is able to match /affiliate.id first since you have
get '/affiliate', to: 'affiliates#affiliate'
It's a legitimate url, with id as the format.
One thing you can do is to move this route after resources :affiliates in routes.rb, so Rails will match affiliate_path first. Or just use the named route in your link_to to remove the ambiguity.
It should be
<td><%= link_to 'Show', affiliate_path %></td>
or
<td><%= link_to 'Show','/affiliate' %></td>
Reason being your have mentioned resources :affiliates which generates url as per REST so
affiliates GET /affiliates(.:format) affiliates#index
so when you go /affiliates it will go to index action which gives you lists of affiliates
I want to make a view for my website coded in RoR wherein all the available path are listed as links. Is there any way to access the views for various models from the program?
For example, something like:
<%model.views.each do |v| %>
<%= link_to v %>
<% end %>
You could use the sitemap_generator or dynamic_sitemaps gem to generate Sitemaps for your application.
You can use named routes, which allows you to create a link to a different part of your rails app, based on names you set in routes.rb. You can also include route parameters, too, which makes it easy to link to models.
In your routes.rb
get 'test_route/:id' => 'example#controller', as :test
In controllers/views:
link_to #example.name, test_path(id: #example.id)
Further reading on named routes
Im not sure why you want this :), but this will give you the routes
Rails.application.routes.routes
if you want to get all the paths as an array
Rails.application.routes.routes.collect {|r| r.path.spec.to_s }
I try to add new controller and model use name foo and foos_controller, hope foos_path can redirect. Doesn't work.
A origin code here (working):
href="<%= contacts_path %>"
After I add new controller and model follow name convention I try use the same (Not working):
href="<%= foos_path %>"
And this contacts_path is not defined anywhere else in rb project.
what does xxxx_path mean and how to use it?
Rails follows convention to handle roots of application
when we execute this command
rails g scaffold foo
it generates routes along with your model, controller and views.
it generates a line in routes.rb as
resources :foo
this line makes you access all the actions of your controller
for example:
foos_path: # redirects you to the index page of your foos controller
new_foo_path: # redirects you to the create page of your foos controller etc.,
please go through this link for reference: http://guides.rubyonrails.org/routing.html
If you go to your terminal and type rake routes, it will list your currently defined routes. On the left side you'll have their prefix. For example, contacts might route to the index action in ContactsController.
The path suffix is a way of referring to those routes inside your code.
If foos_path is giving you an error, that means you either have not yet defined that route for the resource, or you have but it is called something else (if you defined it manually with the as: option).
More info can be found in the Rails Guide for Routing.
You'll be best reading up on this documentation
Path Helpers
Basically, the path_helpers of Rails are designed to help you define routes for your links etc in the most efficient way possible.
The bottom line is the path helper will take routes defined in your config/routes.rb and then allow you to call them dynamically - IE:
#config/routes.rb
resources :photos #-> photos_path
The path names are typically from your controllers, allowing you to route to the various actions inside them. As Rails is built around being a resourceful structure, it will by default create routes for the "standard" controller actions:
link_to
In order to use the path helpers effectively, you'll be best using the rake routes command in cmd, or by simply typing an invalid url into your app's address bar
I notice you're using the standard HTML <a href=""> tag in your question. You'll be much better suited to using the link_to helper of Rails:
<%= link_to "text", your_path %>
in section 5.11 of this rails 4 tutorial. I see this in my view
<td><%= link_to 'Edit', edit_article_path(article) %></td>
edit_article_path which goes to app/views/article/edit.html.erb
and i have a edit method in my article controller. Is this just Rails Magic (will it work in rails 3 too?)
Ie could i do foo_article_path(article) and it would redirect to a app/views/article/foo.html.erb and hit the foo method in my controller ?
I couldnt use this notation to redirect to a page from a different controller correct?
These helpers are created as part of the route definitions. See the following for the details
http://guides.rubyonrails.org/routing.html
For example you probably have something like
resources :articles
in your config/routes.rb file
I have a little problem with my application rails and locale params.
I successfully enabled all my controllers' routes in the routes.rb file with my :locale value between domain and controller in the URL.
When the controller need to show a sorting of many datas from one Table with the method "Data.find(:all)" or "Data.all" or custom condition to see all, I get a routing error.
If there is only une data filtered by an ID or ONE parameter to render only ONE data, all works fine.
In the logs I only see "500 internal error" and the error as I see on my site (development mod).
Here is the full error code:
No route matches {:action=>"edit", :controller=>"translation_english_words", :locale=>#<TranslationEnglishWord id: 1, data: "song", transvalue: "choubidoubop">}
The URL is: "http://domain/fr/translation_english_words" (:domain/:locale/:controller)
My controller is "translation_english_words" and has a table with same name in my database.
The table has 3 column, id:autoincrement, data:string, transvalue:string
I saw the part ":locale" in the route doesn't contain the "fr" or "en" or other locale is must contain. and it just not contain data begining with " and finishing with " as all other params like ":action" and ":controller"
Actually I need locale just for detect the wished language.
Not using any "t(:value)" for translation atm.
This error is not only on this controller, but on EACH controllers that require sorting more than one UNIQUE scoped data from any table.
Someone have any idea about how to solve my problem?
would be great to know the actual implementation of your routes, have you followed the guides? seems you should have something like this:
# config/routes.rb
scope "/:locale" do
resources :translation_english_words
# [...] # all other controllers
end
I'm not sure you
successfully enabled all my controllers' routes in the routes.rb
In fact your controller is interpreted as locale, which makes me think that the routing attempt is different.
Also I can't really understand your url: http://domain/fr/translation_english_words
Shouldn't this be something like": http://domain.lvh.me:3000/fr/translation_english_words if you're working in a local development environment?
Ok everyone, I found "why" the route failed.
It seem this caused the route to break:
Controller:
<pre>
def index
#translation_english_word = TranslationEnglishWord.all
end
</pre>
View:
<pre>
<% for translation_english_word in #translation_english_word %>
<%= link_to "Edit",
edit_translation_english_word_path(translation_english_word) %><br />
<% end %>
<%= link_to "New English Translation", new_translation_english_word_path %>
</pre>
The paths that call edit_translation_english_word_path and new_translation_english_word_path seem to break the route.
I don't know Why, but removing them, resolved my issue, but it still isn't clear why this happens, and I'd like to understand why if anyone can comment to me?