Rails routing name convention - ruby-on-rails

For some reason the conventional path names for a particular controller are not working? (Rails 3.1)
I created a controller using ryan bates nifty scaffold. Just a controller, no underlying model.
in the controller I have
class ProjectTemplatesController < ApplicationController
# a bunch of stuff
def new
#project = Project.new
end
#more stuff
end
in my view (app/views/project_templates/index.html.erb) I have:
<p><%= link_to "New Project Templates", new_project_templates_path %></p>
however I get the error
undefined local variable or method `new_project_templates_path' for #<#<Class:0x2ab9c24>:0x2ab80e0>
in my routes.rb file I declared the controller as a resource like all the others
resources :project_templates
If I change the link to use
<%= link_to "New Project Templates", {:controller=>"project_templates, :action=>"new"} %>
then it works perfectly?! Why doesnt the naming convension of action_controller_path work in this case?

A simple addendum to the previous posts noting the possibility to run rake routes. If on a POSIX system, run the result through grep or some other text filter.
rake routes | grep project
or
bundle exec rake routes | grep project
Generated routes can get to be very plentiful in large projets so I suggest becoming a command line tools guru in order to efficiently data crunch.

You can find all of the routes and their names on the command line with rake routes.
Rails knows about the pluralization you are using so it could be magically removing it and naming the route new_project_template_path without the 's'?

If you are using resources :project_templates, you could safely to do something like this:
link_to "All Project Templates", :project_templates
link_to "New Project Template", [:new, :project_template]
link_to "Edit Project Template", [:edit, #project_template]
link_to "Show Project Template", #project_template
link_to "Delete Project Template", #project_template, :method => :delete
form_for ProjectTemplate.new do |f|
form_for #projectTemplate do |f|
This way, you don't have to remember the plural or singular problem.
One more thing is that you actually could wrap them all in [] so that you don't worry about them.
link_to "All PT", [:project_templates]
link_to "Show PT", [#project_template]

Related

Undefined method in controller (Routing)

I'm new at rails and I'm currently working on an already existing application that handles butons like so:
<%= link_to 'Edit', edit_answer_path(ans) %>
That links to the file /answers/edit.html.erb but now I need to make a button that links to the file /answers/comment.html.erb how do I go about doing this?
I already tried with
<%= link_to 'Comment', comment_answer_path(ans) %>
but I get the error "Undefined method 'comment_answer_path'" even after adding this lines to answers_controller :
def comment
ans = Answer.find(params[:id])
end
You need to add a route to your config/routes.rb and then restart the server. Something like
resources :answers do
member do
get 'comment'
end
end
will create the comment_answer_path helper for you as well.
It depends on how you've set up the routes in routes.rb.
You can use rake routes to see the list of all paths and their alias.

How do you create a link to a .html.erb file in Ruby on Rails?

Ive created a new about page in Ruby on Rails called about.html.erb. How do I create a link to it?
Here is another link I have, Im just not how to write another one for myself.
<%= link_to "Learn more", new_property_path, class: "btn btn-home" %>
check out documentation for #link_to. If you are not sure about second argument you can run bundle exec rake routes and check what url helper you need to use.
You'd probably need to create a route in config/routes.rb for this new "about" action.
An example of route would be:
get "about" => "<name_of_controller>#about", as: "about"
PD: Remember to change the to the actual controller's name.
Then you will be able to use a link like this:
<%= link_to "About", about_path, class: "btn btn-home" %>
<%= link_to "Learn more", new_property_path, class: "btn btn-home" %>
So it's quite straight forward - the first is the name, the last is the css class, you can and probably should reuse these to maintain visual consistency. The middle bit is the piece that will confuse you.
the path comes from your routes.rb file - which is in the config directory. You will need to have a path set up there to use it in the link.
You can run 'rake routes' to see the current mappings that you have. you get a format of:
new_user_account GET /user/new(.:format) user#new
They key to them is that you add path to the first item - so 'new_user_account_path' and it will point to 'user#new' which is the new method in the user controller.
To add routes, you edit the file, and while there are quite a few ways of doing things, your basic CRUD operations are covered by a line of:
resources :users
This will give you more info:
http://guides.rubyonrails.org/routing.html is a guide to this

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.

Move one page to another in Ruby on rails?

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

Rails choosing wrong route when looping through a nested resource

I've got two resources, Clients and Projects.
In routes.rb:
resources :clients do
resources :projects
end
rake routes gives me this the route prefix client_project for the projects#show action
In my view, I call:
<% #client.projects.each do |project| %>
<%= link_to project.name, project %>
<% end %>
and Rails keeps throwing an error: undefined method project_path, which tells me for some reason the view is trying to call project_path and not client_project_path. I've tried rebooting my server (even rebooting my computer), and can't seem to find why it won't call the route properly. I recently refactored my routes (which caused this break). Am I missing something here? This exact pattern works in every other model in my app, only the refactored route doesn't.
I've never had success getting nested routes to work by simply passing the object in question as the url parameter of link_to... Plus (and maybe it's just me) I like my templates to be a bit more "explicit" than that.
A couple things you could do:
<%= link_to project.name, url_for([#client, project]) %>
or
<%= link_to project.name, client_project_path(#client, project) %>
If projects is a nested route it won't have a project_path route unless you explicitly define one. The Project URLs require a Client. Check this out http://guides.rubyonrails.org/routing.html#nested-resources

Resources