如果使用强参数缺少所需的参数,Rails 服务器将使用 HTTP 500 进行响应。
这并不能让我控制向用户反馈到底出了什么问题。能够向他们发送缺少所需参数的消息是否没有意义?
提供适当用户反馈的“Rails 方式”是什么ActionController::ParameterMissing?是否应该捕获异常并在那里处理您的请求响应?在每个控制器中都这样做似乎是错误的。
我正在开发一个带有名为 element 的 atter_accessor 对象的模型。我想将表单数据数组传递给元素对象。在 Rails 控制台中,我收到“未经允许的参数”错误。
Parameters: {"authenticity_token"=>"[FILTERED]", "category"=>{"name"=>"asfd", "body"=>"asf", "element"=>{"1"=>"asfd:text", "2"=>"asfd:text", "3"=>"asfd:text"}}, "type"=>"text", "commit"=>"Create Category"}
Unpermitted parameter: :element. Context: { controller: CategoriesController, action: create, request: #<ActionDispatch::Request:0x0000000106b3ff68>, params: {"authenticity_token"=>"[FILTERED]", "category"=>{"name"=>"asfd", "body"=>"asf", "element"=>{"1"=>"asfd:text", "2"=>"asfd:text", "3"=>"asfd:text"}}, "type"=>"text", "commit"=>"Create Category", "controller"=>"categories", "action"=>"create"} }
Run Code Online (Sandbox Code Playgroud)
模型中 attr_accessor :elements
在控制器中
def category_params
params.require(:category).permit(:name, :body, :elements => [])
end
Run Code Online (Sandbox Code Playgroud)
我尝试了很多替代方案,也将其更改:elements为element: [],但没有任何效果。我想我在这里遗漏了一些东西,这就是我得到未经允许的参数的原因。
使用Ruby 1.9.3,Rails 3.2.13,Strong_parameters 0.2.1:
我已经按照教程和railscast中的每个指示进行了操作,但是我无法使用strong_parameters.它应该是非常简单的东西,但我看不出错误在哪里.
配置/初始化/ strong_parameters.rb:
ActiveRecord::Base.send(:include, ActiveModel::ForbiddenAttributesProtection)
Run Code Online (Sandbox Code Playgroud)
配置/ application.rb中
config.active_record.whitelist_attributes = false
Run Code Online (Sandbox Code Playgroud)
应用程序/模型/ product.rb
class Product < ActiveRecord::Base
end
Run Code Online (Sandbox Code Playgroud)
应用程序/控制器/ products_controller.rb:
class ExpedientesController < ApplicationController
...
def create
@product = Product.new(params[:product])
if @product.save
redirect_to @product
else
render :new
end
end
end
Run Code Online (Sandbox Code Playgroud)
这会像预期的那样引发Forbidden Attributes异常.但是当我搬到:
...
def create
@product = Product.new(product_params)
# and same flow than before
end
private
def product_params
params.require(:product).permit(:name)
end
Run Code Online (Sandbox Code Playgroud)
然后,如果我转到表单并输入"名称:产品1"和"颜色:红色",则不会引发异常; 新产品保存在数据库中,没有颜色,但名称正确.
我究竟做错了什么?
我尝试在rails 4中使用carrierwave保存图像文件,但是当提交按钮时单击数据库是否回滚??? 为什么?
如果我不选择图像上传,只发送字符串,所有工作正常但如果我发送图像文件我得到这样的错误:
TypeError: can't cast ActionDispatch::Http::UploadedFile to string: INSERT INTO "items" ("created_at", "description", "image", "name", "store_id", "sub_items", "title", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?)
(0.2ms) rollback transaction
*** ActiveRecord::StatementInvalid Exception: TypeError: can't cast ActionDispatch::Http::UploadedFile to string: INSERT INTO "items" ("created_at", "description", "image", "name", "store_id", "sub_items", "title", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?)
Run Code Online (Sandbox Code Playgroud)
我的看法 :
<%= form_for @items, :url => dashboard_create_items_path(@store.id), :html => {:multipart => true} do |f| %>
<%= f.label …Run Code Online (Sandbox Code Playgroud) 我有一个标准CRUD方法的用户控制器.其中一种方法是更新.
def update
if @user.update(user_params)
redirect_to @user, notice: "Account Successfully updated"
else
render :edit
end
end
Run Code Online (Sandbox Code Playgroud)
然后我有轨道4强params方法:
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation, :role_ids => [])
end
Run Code Online (Sandbox Code Playgroud)
我的观点是这样的:
<% if is_admin?(current_user) %>
<%= hidden_field_tag "user[role_ids][]", nil %>
<% Role.all.each do |role|%>
<%= check_box_tag "user[role_ids][]", role.id, @user.role_ids.include?(role.id) %>
<%= role.name %><br>
<% end %>
<% end%>
</br></br>
<footer>
<nav>
<%= link_to "Update User", user_path(@user), :method => :put%>
</nav>
</footer>
Run Code Online (Sandbox Code Playgroud)
当我单击视图中的更新用户链接时.我收到此错误:
ActionController::ParameterMissing in UsersController#update
param not found: user
Run Code Online (Sandbox Code Playgroud)
错误的突出显示行是:
private
def …Run Code Online (Sandbox Code Playgroud) 用户模型:
has_and_belongs_to_many :events
Run Code Online (Sandbox Code Playgroud)
活动模式:
has_and_belongs_to_many :users
Run Code Online (Sandbox Code Playgroud)
用户控制器:
params.require(:user).permit(:role, {:event_ids => []})
Run Code Online (Sandbox Code Playgroud)
事件控制器:
params.require(:event).permit(:subject, :location, :date, :time, :all_day, :reminder, :notes, {:users_ids => []})
Run Code Online (Sandbox Code Playgroud)
我的表格:
<%= simple_form_for(@event) do |f| %>
<%= f.input :subject %>
<%= f.input :location %>
<%= f.input :date %>
<%= f.input :time %>
<%= f.association :users %>
<%= f.input :all_day %>
<%= f.input :reminder %>
<%= f.input :notes %>
<%= f.button :submit, :class =>"btn btn-default" %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
我希望能够从列表中选择用户并将该用户保存到数据库中.但是当我尝试创建一个新事件时,我在控制台中收到以下消息:
Unpermitted parameters: user_ids
Run Code Online (Sandbox Code Playgroud)
我的设置不正确吗?我没有将正确的实例变量传递给表单吗?
在下面的例子中,我试图使用强参数.我想要求email_address, password和允许remember_me领域.
但是使用如下所示它只允许方法Ex中的LAST行: - 在下面的情况下它只需要params.permit(:remember_me)
private
def registration_params
params.require(:email_address)
params.require(:password)
params.permit(:remember_me)
end
Run Code Online (Sandbox Code Playgroud)
另一个例子: - 在下面这种情况下,如果我重新安排它如下,它将只采取params.require(:email_address)我出错的地方?
def registration_params
params.require(:password)
params.permit(:remember_me)
params.require(:email_address)
end
Run Code Online (Sandbox Code Playgroud)
更新 Params哈希就像
{
"utf8" => "?",
"email_address" => "test1@gmail.com",
"password" => "password123",
"remember_me" => "true",
"commit" => "Log in",
"controller" => "registration",
"action" => "sign_in"
}
Run Code Online (Sandbox Code Playgroud) ruby model-view-controller ruby-on-rails strong-parameters ruby-on-rails-4
我有以下参数,无法获得强大的参数.
这是我的基本代码,为了简单起见,可以在Rails控制台中运行:
json = {
id: 1,
answers_attributes: {
c1: { id: "", content: "Hi" },
c2: { id: "", content: "Ho" }
}
}
params = ActionController::Parameters.new(json)
Run Code Online (Sandbox Code Playgroud)
我读过的所有内容都说下面的内容应该可行,但它只给了我id一个空的哈希answers_attributes:
params.permit(:id, answers_attributes: [:id, :content])
=> { "id"=>1, "answers_attributes"=>{} }
Run Code Online (Sandbox Code Playgroud)
如果我代之以手动列表c1和c2(如下所示)它可以工作,但这真的很愚蠢,因为我不知道用户将提供多少答案,这是很多重复:
params.permit(:id, answers_attributes: { c1: [:id, :content], c2: [:id, :content] })
=> {"id"=>1, "answers_attributes"=>{"c1"=>{"id"=>"", "content"=>"Hi"}, "c2"=>{"id"=>"", "content"=>"Ho"}}}
Run Code Online (Sandbox Code Playgroud)
我试图取代c1,并c2用0和1,但我仍然需要手工提供0,并1在我的permit语句.
如何允许未知长度的嵌套属性数组?
我正在使用Ember和ember-data以及rails api.我有一个createRecord()和save()用于正常工作的记录.用于在rails中创建记录的post请求的网络选项卡中的有效负载如下所示:{data: {attributes: { foo: 'bar' } }.
在轨道控制器中,我有如下所示的强大的参数:params.require(:data).require(:attributes).permit(:foo),它工作正常一段时间.现在,当我发送请求时,rails表示param is missing or the value is empty: data.如果我查看浏览器中的网络选项卡,请求的有效负载仍然与上述相同.如果我puts params只显示{"controller": "api/v1/answers", "action": "create"}并且根本没有显示数据有效负载.
现在有没有理由为什么导轨没有从ember的右侧参数中获取?我确实尝试为我正在尝试创建的模型添加关联,这是它开始失败的时候.但是,我在它工作时回滚,但它不再工作了.
我有一个字段otp_set_up,在company_user模型中允许为"true"或"false".
有一个用例,sys管理员用户可以将此字段重置为"false".
虽然可以通过代码将字段设置为"true",但是用户可以通过表单编辑等将其设置为"true".
我没有在模型中添加验证,因为它可以是"true"或"false".
在params.require .permit位之前,我在params方法中有以下代码,特定于控制器中的更新:
if curr_company_user.is_sys_admin? && curr_company_user.can_crud_company_users? && params[:id].to_i != curr_company_user.id
params[:company_user] = params[:company_user].except(:otp_set_up) if params[:company_user][:otp_set_up] == true
params.require(:company_user).permit(:otp_setup, etc. etc....
elsif etc. etc...
Run Code Online (Sandbox Code Playgroud)
这有效.系统管理员用户无法将otp_set_up设置为"true".
我的问题是:
这是在Rails中执行此操作的最佳和正确方法吗?对我来说这似乎有些笨拙,通过params hash并删除一点.
有更好/更清洁的方式吗?