簡體   English   中英

Ruby on Rails 4的attr_accessible

[英]Ruby on rails 4 attr_accessible

我正在嘗試訪問數據庫並輸入一些信息。 它給我一個錯誤,說“ ## PostsController:0x00000005aad728>的未定義局部變量或方法`post_params'”

我知道這里已經回答 但是我試着按照他們的所作所為,但這根本行不通,有人可以幫我這個忙嗎?

class PostsController < ApplicationController
  def index
     @post = Post.all     
  end

  def show
      @post = Post.find(params[:id])
  end

  def new
      @post = Post.new
  end

  def create
      @post = Post.new(post_params)

      if @post.save
          redirect_to posts_path, :notice => "Your post was saved"
      else
          render ="new"
      end

      private
      def post_params
         params.require(:post).permit(:title, :content)
      end
  end
end

create方法的endprivate關鍵字和post_params方法。 更新為:

def create
      @post = Post.new(post_params)

      if @post.save
          redirect_to posts_path, :notice => "Your post was saved"
      else
          render ="new"
      end
end # Add end here

private
def post_params
     params.require(:post).permit(:title, :content)
end
end # Remove this end

您可以在create方法中定義post_params方法。 將其移至其外部,一切將正常進行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM