开发者

linking to method in rails via link_to function

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开发者_JAVA百科 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:

get :help, :on => :member, :as => :help
  • The first parameter is :help, that's the name of the action that will be invoked.
  • The second part is :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.
  • The third part, :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:

%li= link_to 'quick help', help_project_path(@project)

Note that you don't really need the :as => :help path, it should be help by 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 help action on the projects controller. 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.


What does the help method do? Is it a help method for each project, or it is help for projects in general. You can create a route as follows:

match '/projects/help/:id' => 'projects#help', :via => :get

for :via, you choose a REST method i.e. either GET, POST, PUT, etc, as per your requirement.

If you are trying to create a help page for projects in general, this might not be useful. Whenever you have a URL as /projects/<>, Rails will try to route it to a project show page, and tries to use <> as ID of the project.

The link_to that you are using, with project_path(@project) will take you to the project show page for the object @project.

So, finally, this is what you should do. I would advise you to create a separate controller, which is used for general pages. Say, you have a controller OtherController

match '/help', :controller => 'other', :action => 'help', :via => :get

And then create a help.html.erb page for the view. Hope this works for you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜