问题
This is the code of 'shared/subnav' partial. When i click on a link it shows error No route matches {:action=>"show", :controller=>"location"}
but routes are defined. i think there is some problem in below code.
-if current_page? location_path
= link_to 'Edit Location', edit_location_path
-if current_page? user_path
= link_to 'Edit User', edit_user_path
-if current_page? alert_path
= link_to 'Edit Alert', edit_alert_path
Here are my routes
location GET /locations/:id(.:format) locations#show
user GET /users/:id(.:format) users#show
alert GET /alerts/:id(.:format) alerts#show
回答1:
Routes
The bottom line is that since you're defining your routes as member routes, you need to ensure you are passing the appropriate id to each of them:
#config/routes.rb
resources :users, only: [:show, :edit]
resources :locations, only: [:show, :edit]
resources :alerts, only: [:show, :edit]
This means you have to pass an :id
value to any of these routes -- which can be done as follows:
user_path("2")
--
Error
The error is clearly being created here:
-if current_page? location_path
As mentioned above, you need to pass a valid "id" to the path, allowing it to pull the required object. You'll need to do the following:
-if current_page? location_path("2")
More pressing, though, are your individual calls to these methods. Surely there must be a better way to manage how this logic is defined. I would try the following:
--
Helper
I think I'd make a helper like this:
#app/helpers/your_helper.rb
Class YourHelper
def edit_current(controller, object)
current = controller.singularize
return link_to "Edit #{current}", eval("edit_#{current}_path(object)")
end
end
This should allow you to call:
<%= edit_current(controller_name, @user) %>
回答2:
As per your routes, you don't have routes for edit
actions of location, user and alert. You have routes for show
actions, so add routes for edit
for all the three entities and then you need to pass an object which you want to edit:
-if current_page? location_path
= link_to 'Edit Location', edit_location_path(current_location)
-if current_page? user_path
= link_to 'Edit User', edit_user_path(current_user)
-if current_page? alert_path
= link_to 'Edit Alert', edit_alert_path(current_alert)
current_location
, current_user
, current_alert
are the objects that you want to edit.
回答3:
Your route helpers are defined, but are expecting an argument. For instance, edit_user_path
is expecting to be passed a user
object, so it knows which user you want to edit.
For users, you may be able to get away with something like edit_user_path current_user
, but for other objects you will probably have to pass them in to your partial.
回答4:
Your show path also expecting some id value while comparing with current_page. have a look at below code this will solve your problem.
-if current_page? location_path(current_location or some id)
= link_to 'Edit Location', edit_location_path(current_location or some id)
-if current_page? user_path(current_user or some id)
= link_to 'Edit User', edit_user_path(current_user or some id)
-if current_page? alert_path(current_alert or some id)
= link_to 'Edit Alert', edit_alert_path(current_alert or some id)
来源:https://stackoverflow.com/questions/25240785/detecting-the-current-page-in-navigation-partial-dont-work