PostsController#show中的ActiveRecord :: UnknownAttributeError

时间:2014-08-25 02:12:40

标签: ruby-on-rails ruby-on-rails-4 rails-activerecord

# posts_controller.rb
 def show
   @comment = @post.comments.build
  end!

enter image description here

我通过脚手架生成了comments_controller.rb等。 然后发生了这个错误。

我不知道该怎么做。 我该如何解决这个错误?

#comments_controller.rb
class CommentsController < ApplicationController
  before_action :set_comment, only: [:show, :edit, :update, :destroy]

  # GET /comments
  # GET /comments.json
  def index
    @comments = Comment.all
  end

  # GET /comments/1
  # GET /comments/1.json
  def show
  end

  # GET /comments/new
  def new
    @comment = Comment.new
  end

  # GET /comments/1/edit
  def edit
  end

  # POST /comments
  # POST /comments.json
  def create
    @comment = Comment.new(comment_params)

    respond_to do |format|
      if @comment.save
        format.html { redirect_to @comment, notice: 'Comment was successfully created.' }
        format.json { render action: 'show', status: :created, location: @comment }
      else
        format.html { render action: 'new' }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /comments/1
  # PATCH/PUT /comments/1.json
  def update

    respond_to do |format|
      if @comment.update(comment_params)
        format.html { redirect_to @comment, notice: 'Comment was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /comments/1
  # DELETE /comments/1.json
  def destroy
    @comment.destroy
    respond_to do |format|
      format.html { redirect_to comments_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_comment
      @comment = Comment.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def comment_params
      params.require(:comment).permit(:user_name, :body, :post_id)
    end
end


#posts_controller.rb
# GET /posts
  # GET /posts.json
  def index
    @posts = Post.all
  end

  # GET /posts/1
  # GET /posts/1.json
  def show
   @comment = @post.comments.build
  end

  # GET /posts/new
  def new
    @post = Post.new
  end

  # GET /posts/1/edit
  def edit
  end

  # POST /posts
  # POST /posts.json
  def create
    @post = Post.new(post_params)

    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: 'Post was successfully created.' }
        format.json { render action: 'show', status: :created, location: @post }
      else
        format.html { render action: 'new' }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /posts/1
  # PATCH/PUT /posts/1.json
  def update
    respond_to do |format|
      if @post.update(post_params)
        format.html { redirect_to @post, notice: 'Post was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /posts/1
  # DELETE /posts/1.json
  def destroy
    @post.destroy
    respond_to do |format|
      format.html { redirect_to posts_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_post
        @post = Post.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def post_params
      params.require(:post).permit(:date, :subject, :body, :picture)
    end

    def video
    end
end

2 个答案:

答案 0 :(得分:0)

在进一步详细介绍之前。我假设您正在使用@post在帖子页面上的表单中使用它。而不是你可以使用符号

<%= form_for :comment, method: :post, url: comments_path do |f| %>

或者如果您使用嵌套路线

<%= form_for [@post, :comment], method: :post, url: comments_path do |f| %>

这将提交给控制器以供评论。

另外,请检查您的schema.rb,确保您的评论模型包含post_id

答案 1 :(得分:0)

foriegn_key

问题在于您的comments表格不会出现post_id

当 你在Rails中使用ActiveRecord associations,虽然看起来是神奇的,但它依赖于老式的关系数据库架构。其主要组成部分是primary_key s(用于识别单个记录)和foreign_key s(用于识别关联记录)

-

如果您希望能够将两个模型关联在一起,则需要确保拥有相应的foriegn_keys:

#app/models/post.rb
Class Post < ActiveRecord::Base
   #fields id | title | body | created_at | updated_at
   has_many :comments
end

#app/models/comment.rb
Class Comment < ActiveRecord::Base
   #fields id | post_id | other | attributes | created_at | updated_at
   belongs_to :post
end

最重要的是,您需要确保评论表中有post_id列。如果您不这样做,则应执行迁移以插入它:

#db/migrate/new_migration.rb
Class NewMigration
   def self.up
      add_column :comments, :post_id, :string
   end
end