I recently added a new method (called “help”) to my projects controller (projects_controller.rb), and created a “help.html.haml” in the views directory.
I added the following route in routes.rb:
resources :users, :only => [:new, :create, :edit, :update, :help]
resources :projects, :module => 'users' do
get :help, :on => :member, :as => :help
end
My goal is to create a link on a separate view (let’s call it index.html.haml) that would link to help.html.haml. Note: at present my routes is set up to display help with the following URL: http://localhost:3001/projects/id/help
Unfortunately I don’t know how to write the code to link to the help URL listed above. As of now I have the following code in index.html.haml:
%li= link_to 'quick help', project_path(@project)
but this code only takes me to http://localhost:3001/projects/id. Is it possible to add the “/help” to the URL using the link_to function, or is there a better way to do this?
I sincerely appreciate any help. Thank you so much for your time!
Let’s take a look at the components of the route you’ve defined:
:help, that’s the name of the action that will be invoked.:on => :member, which means that the action is scoped on a specific record. So, you’re going to have to pass an id to invoke it.:as => :help, defines the name of the helper method that you can use to generate the url. That’s what you’re looking for.Basically,
help_project_path(@project)will generate the url you need. So, the link would look like this:Note that you don’t really need the
:as => :helppath, it should behelpby default. That’s generally used if you want a different helper name.Another thing I’d like to point out is that you’re not creating “a link to help.html.haml”, you’re creating a link to the
helpaction on theprojectscontroller. The action can redirect somewhere if it’s necessary, you don’t have to render the view. I’m just saying this, because it sounds like you’re still a bit new to this, sorry if I’ve simply misunderstood.