rails using link_to with namespaced routes

前端 未结 3 783
野趣味
野趣味 2020-12-23 11:57

I\'ve created a set of routes & controllers with the admin namespace, and I was having some issues using the link helpers with these new routes.

I see that there

相关标签:
3条回答
  • 2020-12-23 12:32

    Some methods require a :url option as a parameter, and in those cases you can use url_for to generate the path:

    icon(:url => url_for(:controller => "admin/projects", :action => "edit", :id => @project),
         :type => :edit)
    
    0 讨论(0)
  • 2020-12-23 12:39

    You should see all of your routes listed in rake routes and you can use those by name to get the proper namespacing. Using the automatic detection where you pass in :controller and :action manually won't work as you've discovered.

    If it's listed as new_thing in the routes, then the method is new_thing_path with the appropriate parameters. For instance:

    link_to('New Project', new_admin_project_path)
    link_to('Projects', admin_projects_path)
    link_to(@project.name, admin_project_path(@project))
    link_to(@project.name, edit_admin_project_path(@project))
    link_to(@project.name, admin_project_path(@project), :method => :delete)
    
    0 讨论(0)
  • 2020-12-23 12:53

    If you're using Rails 3, you can use your admin namespace with the variable instead of writing the long helper path name.

    view:

    <td><%= link_to 'Show', [:admin, project] %></td>
    <td><%= link_to 'Edit', [:edit, :admin, project] %></td>
    <td><%= link_to 'Destroy', [:admin, project], confirm: 'Are you sure?', method: :delete %></td>
    

    controller:

    redirect_to [:admin, @project]
    
    0 讨论(0)
提交回复
热议问题