如何从另一个MVC链接到显示页面? [英] How to link_to a show page from another MVC?

查看:100
本文介绍了如何从另一个MVC链接到显示页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我拉起 http://0.0.0.0:3000/activities 时,出现此错误:

I get this error when I pull up http://0.0.0.0:3000/activities:

ActivityController#index中的ActiveRecord :: RecordNotFound无法 用"id" =找到估价 行:@valuation = Valuation.find(params[:id])

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.each do |activity| %>
  <%= render "activities/#{activity.trackable_type.underscore}/#{activity.action}", activity: activity %>
<% end %>

活动/评估/_创建

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.

路线

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)

推荐答案

要将代码链接到您的显示页面,我了解您需要

To link_to your show page with the code I understand you having you need to

  1. resources :activities do resources :valuations end
  2. 由于它是多态的,因此使用trackable来查找id并放置以下行:<%= link_to activity.trackable.name, activity_valuation_path(activity, activity.trackable_id) %>
  3. 将此行从索引中删除:@valuation = Valuation.find(params[:id])
  1. resources :activities do resources :valuations end
  2. 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) %>
  3. Take this line out from your index: @valuation = Valuation.find(params[:id])

希望有帮助!

这篇关于如何从另一个MVC链接到显示页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆