我正在尝试访问数据库并输入一些信息。它给了我一个错误,说“未定义的局部变量或方法`post_params‘用于#< PostsController:0x00000005aad728>”
我知道here已经回答了这个问题。但是我试着跟着他们做的事情,但是这不起作用,有人能帮我吗?
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发布于 2014-03-23 22:56:25
end for create方法包含private关键字和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发布于 2014-03-23 22:56:25
在create方法中定义post_params方法。把它移开,一切都会正常的。
https://stackoverflow.com/questions/22598244
复制相似问题