【问题标题】:new using rails 4 - ActiveModel::ForbiddenAttributesError新使用 rails 4 - ActiveModel::ForbiddenAttributesError
【发布时间】:2014-11-23 13:17:02
【问题描述】:

我正在使用 devise 和 rails 4。我有以下模型:

class Video
  include Mongoid::Document
  include Mongoid::Timestamps

  field :url,                type: String
  field :video_id,           type: String
  field :title,              type: String
  field :description,        type: String
  field :suggested_by_name,  type: String
  field :suggested_by_email, type: String
  field :active,             type: Boolean, default: false

end

使用以下控制器:

class Frontend::VideosController < ApplicationController

  before_action :authenticate_user!, only: [:index, :edit, :update, :destroy]

  def index
  end

  def new
    @videos = Video.new
  end

  def create
    if @person = Video.create(params[:video])
      flash.now[:success] = "Thanks for suggest this video"
      redirect_to root_path
    else
      flash.now[:error] = "Impossible to add this suggestion"
      redirect_to new_frontend_video_path
    end
  end

  def edit
  end

  def update
  end

  def destroy
  end

  private
    def video_params
      params.require(:video).permit(:url, :title, :description, :suggested_by_name, :suggested_by_email)
    end

end

我正在尝试在我的 mongodb 上保存记录,但我得到:

ActiveModel::ForbiddenAttributesError

所以我认为我在使用强参数时犯了一个错误,请帮忙。

【问题讨论】:

    标签: ruby-on-rails mongodb devise


    【解决方案1】:

    create 操作中将params[:video] 更改为video_params

     def create
        if @person = Video.create(video_params)
          flash.now[:success] = "Thanks for suggest this video"
          redirect_to root_path
        else
          flash.now[:error] = "Impossible to add this suggestion"
          redirect_to new_frontend_video_path
        end
      end
    

    您应该 permit 参数,而不是 Rails 提高 ActiveModel::ForbiddenAttributesError

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多