在控制器的创建操作中将2个模型正确关联到另一个模型

时间:2015-10-05 09:09:22

标签: ruby-on-rails

很多问题与我的问题类似,我已经研究过了。复制他们的答案并没有解决我的问题。

我有3个型号:

class Scoreboard < ActiveRecord::Base
  belongs_to :user
  has_many :teams, dependent: :destroy
  has_many :comments, dependent: :destroy
end

class User < ActiveRecord::Base
  has_many :scoreboards, dependent: :destroy
  has_many :comments, dependent: :destroy
end

class Comment < ActiveRecord::Base
  belongs_to :user
  belongs_to :scoreboard
end

将记分板视为用户可以发表评论的文章。我的目标是当我使用表单创建@comment的新实例时,它与User Model(将发布评论的用户)和&amp; Scoreboard model(表单所在的显示页面的记分板)。然后,我希望能够在scoreboard#show页面上呈现部分<%= render @scoreboard.comments.reject(&:new_record?) %>。在该部分(_comment.html.erb)中,我想显示注释主体并显示与该注释相关联的用户名。该名称应该是指向与该名称相关联的用户的个人资料页面的链接(/users/show.html.erb)。

到目前为止,这就是我所拥有的:

注释的迁移显示已创建外部id列:

class CreateComments < ActiveRecord::Migration
  def change
    create_table :comments do |t|
      t.text :body
      t.text :reply
      t.references :user, index: true
      t.references :scoreboard, index: true

      t.timestamps null: false
    end
    add_foreign_key :comments, :users
    add_foreign_key :comments, :scoreboards
  end
end

包含表单和呈现代码的/scoreboards/show.html.erb视图:

<div class= "comment-section">
  <%= form_for [@scoreboard, @comment] do |f| %> 
        <%= render 'shared/error_messages', object: f.object %>
       <%= f.text_field :body, class: "form-control" %>  
        <%= f.submit "Add Comment", class: " form-3 btn btn-primary" %>         
        <% end %>

  <%= render @scoreboard.comments.reject(&:new_record?) %>

 </div>

部分(/comments/_comment.html.erb),我想显示一个链接,其中包含与评论关联的用户名。

 <p>
   <%= #code to display comment.user.name with a link to User Show page %>
   <%= comment.body %>
 </p>

create method的{​​{1}}:

Comments Controller

def create @scoreboard = Scoreboard.find(params[:scoreboard_id]) @comment = @scoreboard.comments.build(comment_params) @comment.user = current_user if @comment.save redirect_to scoreboard_url(@comment.scoreboard_id) else render 'new' end end 的{​​{1}}:

show method

为了显示与评论​​关联的用户的名称,在已呈现的部分(Scoreboards Controller)中,我尝试了代码def show @scoreboard = Scoreboard.find_by_id(params[:id]) @team = @scoreboard.teams.build #this line is for another model not mentioned @comment = @scoreboard.comments.build end ,导致以下错误:{{1名称'为nil:NilClass`。我可以做些什么来解决这个问题所以可以显示用户名?

1 个答案:

答案 0 :(得分:1)

  

它与User(发布评论的用户)和&amp; Scoreboard(表单所在的显示页面的记分牌)

你会做这样的事情:

#config/routes.rb
resources :scoreboards do
   resources :comments, only: [:create]
end

#app/controllers/scoreboards_controller.rb
class ScoreboardsController < ApplicationController
   def show
       @scoreboard = Scoreboard.find parmas[:id]
       @comments = @scoreboard.comments.new
   end
end

#app/controllers/comments_controller.rb
class CommentsController < ApplicationController
   def create
      @scoreboard = Scoreboard.find params[:id]
      @comment = @scoreboard.comments.new comment_params
   end

   private

   def comment_params
      params.require(:comment).permit(:body, :reply, :etc).merge(user_id: current_user.id)
   end
end

这使您能够按如下方式调用@comment.user

#app/views/comments/_comment.html.erb
<%= comment.body %>
<%= link_to "Written by #{comment.user.id}", comment.user %>

您的错误只会由两种可能性引起:

  
      
  1. 您的@comment没有关联用户
  2.   
  3. 您的comments#create操作未将用户填入评论
  4.   

在这两种情况下,问题都在于您的评论没有关联user。因此,您的主要目标是确保您可以根据需要关联它们。

第一步是确保在数据库中正确填充user_id外键。我们将PHPMyAdmin与我们的开发MYSQL数据库一起使用 - 如果你有类似的东西,请查看保存的内容。如果它在那里,则表示您的@comment对象未加载它;否则你不会将它保存到数据库。

如果没有保存,则问题可能出现在params(正在推送允许的那些)上。无论如何,您都需要确保user_id已填充。

如果正在保存,则表示您的调用@comment将不会加载user关联对象。可能的原因是错误的关联,尽管也可能是您的@comment对象没有填充相关数据。