Ruby on rails 多态关联未定义方法&x27;内容';

Ruby on rails 多态关联未定义方法&x27;内容';,ruby-on-rails,ruby,undefined,polymorphic-associations,railscasts,Ruby On Rails,Ruby,Undefined,Polymorphic Associations,Railscasts,我看了这集《铁路骗子》: 现在请看这个: <%= form_for [@commentable, @comment] do |f| %> <% if @comment.errors.any? %> <div class="error_messages"> <h2>Please correct the following errors.</h2> <ul> <% @comm

我看了这集《铁路骗子》:

现在请看这个:

<%= form_for [@commentable, @comment] do |f| %>
  <% if @comment.errors.any? %>
    <div class="error_messages">
      <h2>Please correct the following errors.</h2>
      <ul>
      <% @comment.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.text_area :content, rows: 8 %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>


请更正以下错误。
我是否在估价控制器中做错了什么,比如估价参数

class ValuationsController < ApplicationController
  before_action :set_valuation, only: [:show, :edit, :update, :destroy]
  before_action :logged_in_user, only: [:create, :destroy]

  def index
    if params[:tag]
      @valuations = Valuation.tagged_with(params[:tag])
    else
      @valuations = Valuation.order('RANDOM()')
    end
  end

  def show
    @valuation = Valuation.find(params[:id])
    @commentable = @valuation
    @comments = @commentable.comments
    @comment = Comment.new
  end

  def new
    @valuation = current_user.valuations.build
  end

  def edit
  end

  def create
    @valuation = current_user.valuations.build(valuation_params)
    if @valuation.save
      redirect_to @valuation, notice: 'Value was successfully created'
    else
      @feed_items = []
      render 'pages/home'
  end
end

  def update
    if @valuation.update(valuation_params)
      redirect_to @valuation, notice: 'Value was successfully updated'
    else
      render action: 'edit'
  end
end

  def destroy
    @valuation.destroy
    redirect_to valuations_url
  end

  private
    def set_valuation
      @valuation = Valuation.find(params[:id])
    end

    def correct_user
      @valuation = current_user.valuations.find_by(id: params[:id])
      redirect_to valuations_path, notice: "Not authorized to edit this valuation" if @valuation.nil?
    end

    def valuation_params
      params.require(:valuation).permit(:name, :private_submit, :tag_list, :content, :commentable, :comment)
    end
end


class CommentsController < ApplicationController
    before_filter :load_commentable

    def index
        @comments = @commentable.comments
    end

    def new
        @comment = @commentable.comments.new
    end

    def create
        @comment = @commentable.comments.new(params[:comment])
        @comment.user = current_user
        if @comment.save
            @comment.create_activity :create, owner: current_user
            redirect_to @commentable, notice: "comment created."
        else
            render :new
        end
    end

    def edit
        @comment = current_user.comments.find(params[:id])
    end

    def update
        @comment = current_user.comments.find(params[:id])
        if @comment.update_attributes(params[:comment])
            redirect_to @commentable, notice: "Comment was updated."
        else
            render :edit
        end
    end

    def destroy
        @comment = current_user.comments.find(params[:id])
        @comment.destroy
        @comment.create_activity :destroy, owner: current_user
        redirect_to @commentable, notice: "comment destroyed."
    end

private

    def load_commentable
        resource, id = request.path.split('/')[1, 2]
        @commentable = resource.singularize.classify.constantize.find(id)
    end

    def comment_params
        params.require(:comment).permit(:content, :commentable)
    end
end


class CreateComments < ActiveRecord::Migration
  def change
    create_table :comments do |t|
        t.text :content
        t.belongs_to :commentable, polymorphic: true

      t.timestamps null: false
    end
    add_index :comments, [:commentable_id, :commentable_type]
  end
end

class CreateValuations < ActiveRecord::Migration
  def change
    create_table :valuations do |t|
      t.string :name
      t.boolean :private_submit
      t.references :user, index: true

      t.timestamps null: false
    end
    add_foreign_key :valuations, :users
    add_index :valuations, [:user_id, :created_at]
  end
end
class ValuationController
类估值{where(private_submit:true)}
作用域:public\u submit,->{where(private\u submit:false)}
包含公共活动::模型
跟踪的所有者:->(控制器,模型){controller&&controller.current_user}
范围:随机化,->do
顺序('RANDOM()')。
采取(1)
结束

结束
正如我在注释中猜测的那样,没有为表
注释定义字段
:content

您可以在rails控制台上看到它,如果它使用
false
进行响应,则可以使用:
Comment.new.respond_to?(:content)

如果在迁移过程中遇到问题,请修复它们并从头开始重新生成数据库:删除数据库,删除文件schema.rb和rake db:migrate

如果迁移正常并且schema.rb有问题,也可以这样做


编辑:当我检查您的代码时,我在操作:
创建
更新
中的
注释控制器
中看到您正在使用
参数[:comment]
进行更新。我想您应该使用方法
comment_params

@0v3rc10ck3d更新了带有跟踪的问题。谢谢@谢谢,但那没用。你用的是什么版本的rails?你重启服务器了吗?你能发布你的模型吗?@DickieBoy我正在使用rails
4.2.0.rc3
。我已重新启动服务器。我在上面添加了模型。为了你的兴趣:)我高度怀疑它的rc版本,但我看不到任何不起作用的东西。我的人起作用了!我不知道怎么做的,但它做到了。祝你一切顺利。