【问题标题】:I am getting this error message after submitting a test post: param is missing or the value is empty: content?提交测试帖子后我收到此错误消息:参数丢失或值为空:内容?
【发布时间】:2014-08-28 06:48:58
【问题描述】:

我正在尝试通过我的表单提交测试帖子,但我收到此错误:参数丢失或值为空:内容。我需要“内容”并允许“标题”。在我的应用程序中提交帖子或“想法”时,这两个字段都已填写。我相信这个问题与强大的参数有关。我在互联网上的其他任何地方都找不到答案。这是我的控制器。

class ThoughtsController < ApplicationController
  def index

  end

  def new
    @thought = Thought.new(params[:id])
  end

  def create
    @thought = Thought.new(params[post_params])

    @thought.save

    if @thought.save
        redirect_to @thought 
    else
        render :new
    end
  end


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

end

感谢您的帮助。

【问题讨论】:

  • 请先阅读有关 Rails 的信息。您的控制器有太多问题。新方法不带任何参数,您也正在使用 id 参数创建新对象。您的强参数也没有以正确的方式定义。 post_params 不能在 create 方法中使用 params[post_params]
  • 提交的参数是什么? (您会在日志文件中找到它们)
  • 我相信只是内容和标题

标签: ruby-on-rails ruby strong-parameters ruby-on-rails-4.1


【解决方案1】:

以下应该有效。您可能理解strong_parameters 有点错误。如果您的思想对象具有:content:title 属性,它们应该列在允许括号中 - 这意味着您允许它们的质量分配。

def create
  @thought = Thought.new(post_params)

    if @thought.save
      redirect_to @post 
    else
      render :new
    end
  end


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

【讨论】:

  • @post 是一个错字,应该是@thought..我应该需要 :thought 而不是 :post 吗?我的意图是制作 :content required。
  • 已编辑答案。是的,看看我提供的链接 - 我相信它会很有用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-03
  • 2015-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多