问题
I get this error when I pull up http://0.0.0.0:3000/activities:
ActiveRecord::RecordNotFound in ActivitiesController#index Couldn't find Valuation with 'id'= Line:
@valuation = Valuation.find(params[:id])
activities_controller
def index
@activities = Activity.order("created_at desc").paginate(:page => params[:page])
@valuation = Valuation.find(params[:id])
end
activities/index
<% @activities.each do |activity| %>
<%= render "activities/#{activity.trackable_type.underscore}/#{activity.action}", activity: activity %>
<% end %>
activities/valuations/_create
added value
<%= activity.created_at.strftime('%B %d at %l:%M%P') %>
<%= link_to activity.trackable.name, valuation_path(@valuation) %> #Trying to Fix This
The goal is so that when users are looking at the activities feed they can click on a activity and be directed to that respective valuation's show page, which is where they can like or comment on it.
routes
resources :activities do
resources :valuations
end
Please let me know if you need any further explanation or code to help you help me otherwise here's the gist of it :)
development.log
Started GET "/activities" for 127.0.0.1 at 2015-06-03 14:14:49 -0400
Processing by ActivitiesController#index as HTML
[1m[35mUser Load (0.2ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
[1m[36mHabit Load (0.1ms)[0m [1mSELECT "habits".* FROM "habits" WHERE "habits"."user_id" = ?[0m [["user_id", 1]]
[1m[35mActsAsTaggableOn::Tag Load (0.2ms)[0m SELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER('ingrain'))
[1m[36mCACHE (0.0ms)[0m [1mSELECT "habits".* FROM "habits" WHERE "habits"."user_id" = ?[0m [["user_id", 1]]
[1m[35m (0.1ms)[0m SELECT COUNT(*) FROM "habits" WHERE "habits"."user_id" = ? [["user_id", 1]]
[1m[36mValuation Load (0.1ms)[0m [1mSELECT "valuations".* FROM "valuations" WHERE "valuations"."id" = ? LIMIT 1[0m [["id", nil]]
Completed 404 Not Found in 11ms
ActiveRecord::RecordNotFound (Couldn't find Valuation with 'id'=):
app/controllers/activities_controller.rb:4:in `index'
Rendered /Users/galli01anthony/.rvm/gems/ruby-2.1.3/gems/actionpack-4.2.0.rc3/lib/action_dispatch/middleware/templates/rescues/_source.erb (9.3ms)
Rendered /Users/galli01anthony/.rvm/gems/ruby-2.1.3/gems/actionpack-4.2.0.rc3/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (5.2ms)
Rendered /Users/galli01anthony/.rvm/gems/ruby-2.1.3/gems/actionpack-4.2.0.rc3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.6ms)
Rendered /Users/galli01anthony/.rvm/gems/ruby-2.1.3/gems/actionpack-4.2.0.rc3/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (126.4ms)
Started GET "/activities" for 127.0.0.1 at 2015-06-03 14:14:50 -0400
Processing by ActivitiesController#index as HTML
[1m[35mUser Load (0.2ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
[1m[36mHabit Load (0.1ms)[0m [1mSELECT "habits".* FROM "habits" WHERE "habits"."user_id" = ?[0m [["user_id", 1]]
[1m[35mActsAsTaggableOn::Tag Load (0.2ms)[0m SELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER('ingrain'))
[1m[36mCACHE (0.0ms)[0m [1mSELECT "habits".* FROM "habits" WHERE "habits"."user_id" = ?[0m [["user_id", 1]]
[1m[35m (0.1ms)[0m SELECT COUNT(*) FROM "habits" WHERE "habits"."user_id" = ? [["user_id", 1]]
[1m[36mValuation Load (0.1ms)[0m [1mSELECT "valuations".* FROM "valuations" WHERE "valuations"."id" = ? LIMIT 1[0m [["id", nil]]
Completed 404 Not Found in 8ms
ActiveRecord::RecordNotFound (Couldn't find Valuation with 'id'=):
app/controllers/activities_controller.rb:4:in `index'
Rendered /Users/galli01anthony/.rvm/gems/ruby-2.1.3/gems/actionpack-4.2.0.rc3/lib/action_dispatch/middleware/templates/rescues/_source.erb (9.0ms)
Rendered /Users/galli01anthony/.rvm/gems/ruby-2.1.3/gems/actionpack-4.2.0.rc3/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (3.6ms)
Rendered /Users/galli01anthony/.rvm/gems/ruby-2.1.3/gems/actionpack-4.2.0.rc3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.2ms)
Rendered /Users/galli01anthony/.rvm/gems/ruby-2.1.3/gems/actionpack-4.2.0.rc3/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (35.2ms)
回答1:
To link_to your show page with the code I understand you having you need to
resources :activities do resources :valuations end
- Since it's polymorpic use trackable to find the id and put the line:
<%= link_to activity.trackable.name, activity_valuation_path(activity, activity.trackable_id) %>
- Take this line out from your index:
@valuation = Valuation.find(params[:id])
Hopefully that helps!
回答2:
you got nested resources:
resources :activities do
resources :valuations
end
this does not create the method
valuation_path()
it creates
activities_valuation_path()
if you want to create the valuation path you need to refactor to:
resources :activities do
resources :valuations
end
resources :valuations # this will create your method
or if the valuation is strongly connected to an activity i recommend using
activities_valuation_path(:activity_id, :valuation_id)
来源:https://stackoverflow.com/questions/30627538/how-to-link-to-a-show-page-from-another-mvc