ActiveModel :: ForbiddenAttributesError Ruby

时间:2014-12-11 13:31:14

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

我坚持这个错误,我无法弄清楚什么是错的。

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

2 个答案:

答案 0 :(得分:2)

您必须将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允许来自:tickets哈希的params值,并且您已使用permitparams[:tickets]哈希中允许哪些密钥宾语。你放了:title, :text。问题是您使用的params[:ticket]没有允许参数。

答案 1 :(得分:0)

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

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

感谢您的投入。