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
Related
I'm writing a Rails Engine and in one of my views I'd like to create a link to main_app if a named route exists.
I will give an example to clarify the question:
The Main application may or may not define in routes.rb:
resources :my_resource, only: :index
In my rails engine I need to add a link to my_resource_path only if main_app defined that route. I think it should be something like the following:
<%= link_to "Link", main_app.my_path if my_resource_path_exists? %>
I tried:
<%= link_to "Link", main_app.my_resource_path if
main_app.respond_to?(:my_resource_path) %>
but, when the path does not exist, that raises NoMethodError:
undefined method `my_resource_path' for #<Module>
How my_resource_path_exists? method could be implemented? I'm using ruby 2.2 and rails 4.2
The question is related to: Determine if path exists as route in Rails controller
But in this case, I have a named route (my_resource_path), so I need something slightly different, but can't figure out by myself.
I found a solution wich does not require to recover from fail. It's possible to check if the route is defined using url_helpers:
Rails.application.routes.url_helpers.method_defined?(:my_path)
So, the code could be written like that:
<%= link_to "Link", main_app.my_resource_path if
Rails.application.routes.url_helpers.method_defined?(:my_resource_path) %>
You could either use main_app.try(:my_path) or rescue NoMethodError to return false.
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.
In my rails project there is a controller and view named "Welcome/index" where index is an action and another one named "home/page" . As i set root"home#page" as my root page. Now i want to transfer from "page.html.erb" into "index.html.erb" . How can i do that. And the code i written is below.Do i have to enter some thing in my controller class. please suggest.
these are the links that i tried. (How to create an anchor and redirect to this specific anchor in Ruby on Rails)
<a rel="nofollow" href="index.html.erb">Transfer to index</a>
You are not supposed to link to .html.erb files, you should link to the methods (not exactly the name of the method, but the name of the route) of a controller.
I strongly encourage you to review the ruby on rails MVC principles. You can read about routing and linking aswell.
Responding to your question, check out the command "rake routes". It will list the defined routes of your app and helps you to use them.
Try to replace your code by this:
<%= link_to 'welcome', welcome_path %>
<%= link_to "Link", controller:"controllername" %>
is the code you should use
You need to make sure a named route is defined for welcome/index and then can use the Rails helper link_to to automatically build your link for you in the view.
In routes.rb:
match '/welcome' => 'welcome#index', :as => :welcome
In your page.html.erb view:
<%= link_to 'Go to Welcome Page', welcome_path %>
If you want go for index action of Welcome controller then you can use:
<%= link_to "Transfer to index", welcome_path %>
Check the rake routes for path.
Plese refer link
I am having trouble with some Ruby on Rails code. I have a data structure of nested resources (List has_many Tasks and a Task belongs_to a List). On my List.show page, I have the following line of code:
<td><%= link_to 'Show Task', list_tasks_path(#list, task) %></td>
The idea is that this page will link to a "show" page where I can view the details of an individual Task. I want the url to basically be
/lists/:list_id/tasks/:task_id
The problem is that the above code is directing me to
/lists/:list_id/tasks.task_id
on the index page of Task. How can I tell Rails to send me to the show page instead?
I have tried adding :action => show and :controller => tasks, as well as using show_list_tasks_path(#list, task). I also know that the structure itself is there because my show page works fine if I manually enter /lists/:list_id/tasks/:task_id.
Any help is appreciated. Thank you!
-Sum
The problem here is that you're using list_tasks_path when you should be using list_task_path.
The list_tasks_path method will generate a URL to the index action, as you know already, and will make the second argument be the format for this request.
For more information please read the Routing Guide.
A simpler way to specify this url would be
<%= link_to 'Show Task', [#list, task] %>
You can also do list_task_path(#list, task) (notice task instead of tasks).
Run rake routes to see exact names rails generates for your routes.
I have a page app/views/new/news.html.erb and I simply want to link to this page from within my layouts/application.html.erb. Can someone please help! I have been struggling with this all morning.
I have tried things like <%= link_to "News", ... But I'm not really sure where to go from there.
You don't "link" to a view, you link to a controller which renders that view. Then, you'll need an entry in your routes.rb to wire up the url routing for that controller. If you have a controller named NewsController with a method called index and an entry in your routes.rb that looks like resources :news the following link_to should work: link_to "News", news_path.
In case it's not clear, the index method in your NewsController needs to have render :news in it.
Sounds like you may want to check out the guide on this topic: http://guides.rubyonrails.org/routing.html
If you have it setup correctly you should have a plural for your controller (i.e. news instead of new) and the following should work:
<%= link_to 'News', :controller => "news", :action => :news %>
This is assuming you are using scaffold.
If are adding this folder and page manually: for dynamic page, you have to create an action in your controller. For static page, you have to put it in your public folder.
You can always run
rake routes > routes.txt
in your application directory, which will dump a list of all routes into a txt file. Choose path that leads to action and controller you want, and then supply it as a param for link_to method :)