I’ve created in rails(3) a new html page in my project model named contact.html.erb and I am interested in linking to it from a different page, i’ve added the code:
<%= link_to 'contact', contact_project_path(@project) %>
Project is a model that belongs_to a User (which is the contact in the contact page).
This link gives me an error message saying that contact_project_path doesnt exist. I know I need to define it somewhere but I don’t know where or how. I want the page to be specific per project. I’ve created an action in the project_controler named contact and left it empty.
What am i missing?
I’ve searched for a soloution for quite some time and haven’t found an answer to this question anywhere else. I’m aware its a little bit basic but I have no other options.
-REVISED ANSWER-
Based on comments I see you’re not trying to associate a contact model, just a view. In that case, you still need to change the routes file, but you need to decide if you want to get a single contact page for all projects, or one contact view per project. IE:
Either way you need to use a block for your project resource in your routes file. So, if you want there to be one contact page for the collection (all projects), do:
Or, if you want one contact view per member (one per project) do:
Either way this will give you helper methods which you can put into
link_toFor the collection it should be
contact_projects_path(no arguments), and for members it should becontact_project_path(@project)(pass in the project as an argument.You can use
getpostputdeleteormatchas a parameter in a collection or member block, that just tells Rails what kind of request to handle at that URL, and what helpers to generate. For normal views you want a GET request.I hope this finally answers your question 🙂
-ORIGINAL ANSWER-
This is for routing to a MODEL, not just a view
Add to your routes.rb:
If I understand your question correctly, that should create a helper called project_contact_path().
This record will be
projects/123/contact.If you want more than one contact per project, you’ll need to make it plural (resource :contacts). Then your records would look like
projects/123/contacts/123.See http://guides.rubyonrails.org/routing.html
You would link to this using
link_to( project_contact_path(@project) )if there’s only one contact per project, orlink_to( project_contact_path(@project,@contact) )if there are multiple contacts per project.