Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 4 Can';不要忘记ActiveModel::禁止属性错误_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 4 Can';不要忘记ActiveModel::禁止属性错误

Ruby on rails 4 Can';不要忘记ActiveModel::禁止属性错误,ruby-on-rails-4,Ruby On Rails 4,您好,我正在尝试学习Rails,我遵循本教程->介绍如何构建博客页面。我使用Rails 4.1,PostsController是: class PostsController < ApplicationController respond_to :html def index @posts = Post.order("created_at desc") respond_with @posts end def create Post.create(p

您好,我正在尝试学习Rails,我遵循本教程->介绍如何构建博客页面。我使用Rails 4.1,PostsController是:

class PostsController < ApplicationController
  respond_to :html
  def index
    @posts = Post.order("created_at desc")
    respond_with @posts
  end

  def create
    Post.create(params[:post])
    redirect_to posts_path
  end

end
这是必要的,但不确定将该方法放在何处以及应使用哪些参数。看起来所有的答案都是假设我已经知道Ruby和Rails,但我不知道,我是个新手。我需要帮助,谢谢。

试试这个:

# app/controllers/posts_controller.rb
def create
  Post.create(post_params)
  redirect_to posts_path
end

private

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

谢谢你的意见。我试过了,但它抛出了相同的错误消息。这几乎就像Ruby解释器根本不在乎在“create”方法之后或之前写了什么。顺便说一句,抛出的错误消息是:[link]显示的错误消息使用的是您的原始代码。当使用我在答案中所写的代码时,您能显示准确的错误消息吗?我去看看有没有发现什么东西。:)谢谢你的评论。我已经检查了代码并重新编辑了它,现在它正确地包含了您给出的代码。这里是:但它仍然给出相同的错误。我已经在Internet上打开了代码,您可以查看它。谢谢。请尝试将
Post.create(params[:Post])
替换为
Post.create(Post\u params)
谢谢您的评论。更换后,它终于起作用了。这很好,但还有一个问题:post_参数是变量还是函数。我认为或者它看起来是一个函数,对吗?谢谢
params.permit post: [:title, :body]
# app/controllers/posts_controller.rb
def create
  Post.create(post_params)
  redirect_to posts_path
end

private

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