在Michael Hartl的Ruby on Rails教程中获取错误消息:MicropostsController中的NoMethodError #create,如何避免收到此消息?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Michael Hartl的Ruby on Rails教程中获取错误消息:MicropostsController中的NoMethodError #create,如何避免收到此消息?相关的知识,希望对你有一定的参考价值。

无法在我的toy_app中添加或编辑新的微博(Rails教程的第2章)。在我添加用户和微博之前没有问题。然后我指定了has_many / belongs_to关系,自从我得到了:

“MicropostsController中的NoMethodError#为你的意思创建未定义的方法`content'?context context = context?”当我尝试创建和微博时,以及更新微博时的类似信息。

可能没什么值得我只有一个用户,用户ID是“3”...不知道为什么它不会是1.我已经删除了所有的微博,但仍然只有1个用户。如何避免此错误消息?如果有人可以提供帮助,我会非常感激。

Microposts控制器:

    class MicropostsController < ApplicationController
  before_action :set_micropost, only: [:show, :edit, :update, :destroy]

  # GET /microposts
  # GET /microposts.json
  def index
    @microposts = Micropost.all
  end

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

  # GET /microposts/new
  def new
    @micropost = Micropost.new
  end

  # GET /microposts/1/edit
  def edit
  end

  # POST /microposts
  # POST /microposts.json
  def create
    @micropost = Micropost.new(micropost_params)

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

  # PATCH/PUT /microposts/1
  # PATCH/PUT /microposts/1.json
  def update
    respond_to do |format|
      if @micropost.update(micropost_params)
        format.html { redirect_to @micropost, notice: 'Micropost was successfully updated.' }
        format.json { render :show, status: :ok, location: @micropost }
      else
        format.html { render :edit }
        format.json { render json: @micropost.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /microposts/1
  # DELETE /microposts/1.json
  def destroy
    @micropost.destroy
    respond_to do |format|
      format.html { redirect_to microposts_url, notice: 'Micropost was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def micropost_params
      params.require(:micropost).permit(:context, :user_id)
    end
end

Microposts型号:

class Micropost < ApplicationRecord
    belongs_to :user
    validates :content, length: { maximum: 140 }
end

用户型号:

class User < ApplicationRecord
   has_many :microposts
end

用户控制器

class UsersController < ApplicationController
  before_action :set_user, only: [:show, :edit, :update, :destroy]

  # GET /users
  # GET /users.json
  def index
    @users = User.all
  end

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

  # GET /users/new
  def new
    @user = User.new
  end

  # GET /users/1/edit
  def edit
  end

  # POST /users
  # POST /users.json
  def create
    @user = User.new(user_params)

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

  # PATCH/PUT /users/1
  # PATCH/PUT /users/1.json
  def update
    respond_to do |format|
      if @user.update(user_params)
        format.html { redirect_to @user, notice: 'User was successfully updated.' }
        format.json { render :show, status: :ok, location: @user }
      else
        format.html { render :edit }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /users/1
  # DELETE /users/1.json
  def destroy
    @user.destroy
    respond_to do |format|
      format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def user_params
      params.require(:user).permit(:name, :email)
    end
end

schema.rb:

ActiveRecord::Schema.define(version: 20171222133429) do

  create_table "microposts", force: :cascade do |t|
    t.text "content"
    t.integer "user_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "users", force: :cascade do |t|
    t.string "name"
    t.string "email"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

end
答案

在你的micropost_params你允许context。我认为它应该是一个content。请检查您的schema.rb以验证您的microposts表有哪些列。

以上是关于在Michael Hartl的Ruby on Rails教程中获取错误消息:MicropostsController中的NoMethodError #create,如何避免收到此消息?的主要内容,如果未能解决你的问题,请参考以下文章

Michael Hartl Rails 教程第 11 和 12 章:多个错误和邮件未发送

Michael Hartl的Rails教程第11章和第12章:多个错误和邮件未被发送

Faraday::ConnectionFailed, Connection denied - connect(2) for “localhost” port 9200 Error Ruby on Ra

ruby on rials (项目实战技巧)

如何在 Ruby-on-Rails 中生成 PDF 表单

在 Hartl 的教程中添加联系表单