Ruby-on-rails rails 4 ActiveModel::ForbiddenAttributesError

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17868427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:23:13  来源:igfitidea点击:

rails 4 ActiveModel::ForbiddenAttributesError

ruby-on-railsruby-on-rails-4

提问by Maki

I try to do a simple create using rails 4

我尝试使用 rails 4 做一个简单的创建

my controller:

我的控制器:

class AdsController < ApplicationController

  def new
    @ad = Ad.new
  end

  def create
    @ad = Ad.new(params[:ad])    
    @ad.save
  end

  def show
    @ad = Ad.find(params[:id])
  end

  def index
    @ads = Ad.first(3)
  end


  private
  def ad_params    
    params.require(:ad).permit(:title, :price, :description)
  end
end

form:

形式:

<%= form_for @ad do |p| %>
  <p><%= p.text_field :title %></p>
  <p><%= p.text_field :price %></p>
  <p><%= p.text_area :description %></p>
  <p><%= p.submit %></p>
<% end %>

from my point of view it's ok but I got this error ActiveModel::ForbiddenAttributesErrorwhat I'm doing wrong?

从我的角度来看,没关系,但我遇到了这个错误ActiveModel::ForbiddenAttributesError,我做错了什么?

UPDATE:

更新:

my problem was passing wrong value to new method in create action: the solution was to pass ad_paramsto it

我的问题是在创建操作中将错误的值传递给新方法:解决方案是传递ad_params给它

回答by Noah Koch

I would suggest skipping to update so your code works

我建议跳过更新以便您的代码正常工作

Your problem is probably not in your controller, but your model: Check to see your attributes are accessible with the follow tag

您的问题可能不在您的控制器中,而在您的模型中:检查以查看您的属性是否可以使用 follow 标签访问

attr_accessible :title, :price, :description

Rails 4 does do it a little different from what I understand, this previous SO answer provided some good links: How is attr_accessible used in Rails 4?

Rails 4 的做法与我的理解略有不同,之前的 SO 答案提供了一些很好的链接: 如何在 Rails 4 中使用 attr_accessible?

You need to use attr_accessible/Strong Params whenever you're accessing things from the database.

每当您从数据库访问内容时,您都需要使用 attr_accessible/Strong Params。

Update

更新

Oh to be young, and not realize Rails 4 uses Strong Params. I understand the OP has already solved his original problem, but I'm going to correct this so it could actually be used as the right answer.

哦,年轻,没有意识到 Rails 4 使用了强参数。我知道 OP 已经解决了他最初的问题,但我将更正这个问题,以便它实际上可以用作正确答案。

This would be a controller level issue, as Rails 4 requires you to whitelist attributes in the controller.

这将是控制器级别的问题,因为 Rails 4 要求您将控制器中的属性列入白名单。

Ad.create(params[:ad].require(:ad).permit(:title, :price, :description))

Most of the time you'll be permitting the same params in the create and update action, so it's best to move it into its own method within the controller.

大多数情况下,您将允许在 create 和 update 操作中使用相同的参数,因此最好将其移动到控制器中自己的方法中。

Ad.create(ad_params)
def ad_params
  params.require(:ad).permit(:title, :price, :description)
end

As OP pointed out in his comment, the permitted_params method he implemented was not being called from the permit.

正如 OP 在他的评论中指出的那样,他实现的 allowed_pa​​rams 方法不是从许可证中调用的。

回答by Noah Koch

I also faced same problem and worked for 6 hours, and finally got the solution.

我也遇到了同样的问题,工作了 6 个小时,终于得到了解决方案。

Try this Code, it will help you! In Rails 4, this feature is added to deal with creation in different ways.

试试这个代码,它会帮助你!在 Rails 4 中,添加了这个特性来以不同的方式处理创建。

def new
  @ad = Ad.new
end

def create
  @ad = Ad.create(ad_params)
  @ad.save
end

private

def ad_params
  params.require(:ad).permit(:title, :price, :description)
end

回答by u488552

Ad new before add params.permit!

在添加 params.permit 之前添加新广告!

   def create
     params.permit!
     @ad = Ad.new(params[:ad])    
     @ad.save
   end

回答by Lesly Revenge

Your's should look like this:

你的应该是这样的:

def new
    @ad = Ad.new(params[:ad].permit(:title, :price, :description))
end

Mine looks like this:

我的看起来像这样:

def create
    @about = About.new(params[:about].permit(:introduction, :description))

    respond_to do |format|
        if @about.save
            format.html { redirect_to @about, notice: 'About was successfully created.' }
            format.json { render action: 'show', status: :created, location: @about }
        else
            format.html { render action: 'new' }
            format.json { render json: @about.errors, status: :unprocessable_entity }
        end
    end
end