【问题标题】:NoMethodError in CommentsController#new undefined method `text' for #<Comment id: nil, author: nil, created_at: nil, updated_at: nil>CommentsController 中的 NoMethodError #new undefined method `text' for #<Comment id: nil, author: nil, created_at: nil, updated_at: nil>
【发布时间】:2015-05-28 10:48:13
【问题描述】:

我正在尝试将 Rails 与 React 一起使用,只需将 cmets 部分添加到我正在开发的应用程序中即可。 /comments 页面有效。但是当我尝试发表新评论时。我得到这个错误。我回到了我正在使用的指南,并没有对此做出任何解释。我是 Rails 的新手并且会做出反应,所以如果有人可以帮助我,我将不胜感激。

应用程序跟踪:

app/views/comments/_form.html.erb:20:in `block in_app_views_comments__form_html_erb__930297241_104622300'
app/views/comments/_form.html.erb:1:in `_app_views_comments__form_html_erb__930297241_104622300'
app/views/comments/new.html.erb:3:in `_app_views_comments_new_html_erb__151456299_104688260'

cmets_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 :show, status: :created, location: @comment }
            else
                format.html { render :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 { render :show, status: :ok, location: @comment }
            else
                format.html { render :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, notice: 'Comment was successfully destroyed.' }
        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(:author, :comment_text)
        end
end

_form.html.erb:

<%= form_for(@comment) do |f| %>
<% if @comment.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@comment.errors.count, "error") %> prohibited this comment from being saved:</h2>

<ul>
    <% @comment.errors.full_messages.each do |message| %>
        <li><%= message %></li>
    <% end %>
</ul>
</div>
<% end %>

<div class="field">
    <%= f.label :author %><br>
    <%= f.text_field :author %>
</div>
<div class="field">
    <%= f.label :comment_text %><br>
    <%= f.text_area :comment_text %>
</div>
<div class="actions">
    <%= f.submit %>
</div>
<% end %>

new.html.erb:

<h1>New Comment</h1>

<%= render 'form' %>

<%= link_to 'Back', comments_path %> 

[时间戳]_create_cmets.rb:

class CreateComments < ActiveRecord::Migration
    def change
        create_table :comments do |t|
            t.string :author
            t.text :comment_text

            t.timestamps null: false
        end
    end
end 

【问题讨论】:

    标签: html ruby-on-rails ruby-on-rails-4 reactjs erb


    【解决方案1】:

    似乎问题源于您的架构/迁移。行:

    params.require(:comment).permit(:author, :text)
    

    建议您有一个名为 text 的列,它是 Rails 中的一种数据类型。您应该为该列选择一个不回显数据类型名称的名称,例如“comment_text”。

    您也可能在迁移中转置了数据类型和列名。

    【讨论】:

    • 我已经销毁了 Comment 脚手架,并使用 comment_text 列重新生成了它。但现在错误是undefined method comment_text
    • 请发布迁移文件。您还必须在控制器中编辑强参数方法: params.require(:comment).permit(:author, :comment_text)
    • 我注意到由于某种原因我不能同时拥有迁移文件并且能够运行迁移命令。当我删除迁移文件并运行迁移时,我收到上面发布的错误,但它不会生成迁移文件。当我保留迁移文件并运行迁移终端时,终端给我一个重复表错误,网页给我一个错误,告诉我运行迁移命令。
    • 迁移文件中的所有内容似乎都井井有条。如果您的数据库中没有您希望保留的任何内容,此时,我建议您执行 rake db:drop db:create db:migrate 并查看效果如何。对已经运行迁移的数据库运行迁移会导致重复表错误。删除数据库将带您解决问题并避免该特定问题。
    • 我尝试了 db:drop 但我得到了PG::ObjectInUse: ERROR: database &lt;db_name&gt; is being accessed by other users DETAIL: There is 1 other session using the database. 我尝试通过查找来解决此错误,但我无法修复它。
    猜你喜欢
    • 2022-11-08
    • 1970-01-01
    • 2018-03-13
    • 2013-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-05
    • 1970-01-01
    相关资源
    最近更新 更多