如何链接到另一个MVC的节目页面?

时间:2015-06-03 18:07:31

标签: ruby-on-rails ruby model-view-controller feed link-to

当我提起http://0.0.0.0:3000/activities时,我收到此错误:

  

活动控制器中的ActiveRecord :: RecordNotFound #index couldn&t; t   使用' id' =查找估价值   行:@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 %>

活动/估值/ _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

目标是,当用户查看活动供稿时,他们可以点击活动并转到相应评估的展示页面,这是他们可以喜欢或评论的地方。

路由

resources :activities do
  resources :valuations
end

如果您需要任何进一步的解释或代码来帮助我,请告知我们,否则 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)

2 个答案:

答案 0 :(得分:1)

你有嵌套资源:

resources :activities do
  resources :valuations
end

这不会创建方法

  

valuation_path()

它创建

  

activities_valuation_path()

如果要创建需要重构的评估路径:

resources :activities do
  resources :valuations
end
resources :valuations            # this will create your method

或者估价与我建议使用

的活动紧密相关
  

activities_valuation_path(:activity_id,:valuation_id)

答案 1 :(得分:1)

使用我了解您需要

的代码链接到您的节目页面
  1. resources :activities do resources :valuations end
  2. 由于它的多态使用可以跟踪查找id并放置行:<%= link_to activity.trackable.name, activity_valuation_path(activity, activity.trackable_id) %>
  3. 从索引中取出这一行:@valuation = Valuation.find(params[:id])
  4. 希望这有帮助!