【问题标题】:ActiveModel::ForbiddenAttributesError RubyActiveModel::ForbiddenAttributesError Ruby
【发布时间】:2014-12-11 13:31:13
【问题描述】:

我被这个错误困住了,我不知道出了什么问题。

class TicketsController < ApplicationController
  def new
  end

  def create
    @tickets = Newticket.new(params[:tickets])

    @tickets.save
    redirect_to @tickets
  end

  private

  def tickets_params
    params.require(tickets).permit(:title, :text)
  end
end

【问题讨论】:

标签: ruby-on-rails ruby ruby-on-rails-4


【解决方案1】:

您必须将ticket_params 传递给new 方法

def create
    # your code
    @tickets = Newticket.new(params[:tickets])

    # you should use
    @tickets = Ticket.new(ticket_params) #if your model is named Ticket
    @tickets = Newticket.new(ticket_params) #if your model is named Newticket
    @tickets.save
    redirect_to @tickets
end

private
    def tickets_params
        params.require(:tickets).permit(:title, :text) # if your model is named Ticket
        params.require(:newtickets).permit(:title, :text) # if your model is named Newticket
    end
end

您有创建方法ticket_params 允许来自params 哈希的:tickets 值,并且您已经用permit 说在params[:tickets] 哈希对象中允许哪些键。您已输入:title, :text。您的问题是您使用的 params[:ticket] 没有任何允许的参数。

【讨论】:

  • 我认为应该是 Ticket.new(ticket_params)
  • 如果model当然是Ticket,但是他要求model Newticket
  • 我认为他没有 Ticket 和 Newticket(顺便说一句,它会被命名为 NewTicket)模型。
【解决方案2】:

@tickets = Newticket.new(params[:tickets])

Newticket.new 那个是造成麻烦的人。我之前在生成控制器时定义了错误的方法,我称之为 Newticket,而我应该称之为门票。

感谢您的输入。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多