首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rails 4 attr_accessible

rails 4 attr_accessible
EN

Stack Overflow用户
提问于 2014-03-23 22:54:09
回答 2查看 137关注 0票数 0

我正在尝试访问数据库并输入一些信息。它给了我一个错误,说“未定义的局部变量或方法`post_params‘用于#< PostsController:0x00000005aad728>”

我知道here已经回答了这个问题。但是我试着跟着他们做的事情,但是这不起作用,有人能帮我吗?

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-03-23 22:56:25

end for create方法包含private关键字和post_params方法。更新如下:

代码语言:javascript
复制
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
票数 4
EN

Stack Overflow用户

发布于 2014-03-23 22:56:25

在create方法中定义post_params方法。把它移开,一切都会正常的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22598244

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档