[英]How to use Strong_Parameters in Rails 4 with Mongoid, if it throws Unpermitted parameters: (model_name) error on nested attributes
I am trying to use embedded relationship in MongoDB by using Mongoid with Rails 4.1.6. 我正在尝试通过在Rails 4.1.6中使用Mongoid在MongoDB中使用嵌入式关系。 I have embedded an array of location data in my user model, as I will be adding user's location after every 5 minutes of interval. 我已经在用户模型中嵌入了一系列位置数据,因为每隔5分钟就会添加一次用户位置。
This issue is: 这个问题是:
I can add data to my database using IRB or Mongo Shell. 我可以使用IRB或Mongo Shell将数据添加到数据库中。 But when I use my applications front end, it doesnt pass the embedded attributes. 但是,当我使用应用程序前端时,它不会传递嵌入式属性。 Ultimately document contains only user name which is the field of user model. 最终,文档仅包含用户名,这是用户模型的字段。
I tried to put the user_params in consol then I saw the below error : 我试图将user_params放在consol中,然后看到以下错误:
Started POST "/users" for 127.0.0.1 at 2014-10-21 05:04:04 +0100 Processing by UsersController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"S5QKXuzEZID7eEjfhVWcYyLjEpi+Y3NVWt9cy/aEKOc=", "user"=>{"name"=>"Appuvaa4", "coord"=>{"lat"=>"40.23", "long"=>"33.33", "alt"=>"33.22"}}, "commit"=>"Create User"} Unpermitted parameters: coord 在2014-10-21 05:04:04 +0100由UsersController#create作为HTML参数开始为127.0.0.1启动POST“ /用户” Y3NVWt9cy / aEKOc =“,”用户“ => {”名称“ =>” Appuvaa4“,” coord“ => {” lat“ =>” 40.23“,” long“ =>” 33.33“,” alt“ => “ 33.22”}},“ commit” =>“创建用户”}不允许的参数:coord
Unpermitted parameters: coord {"name"=>"Appuvaa4"} 不允许的参数:coord {“ name” =>“ Appuvaa4”}
I cant understand why it says Unpermitted parameters: coord, however I have used rails strong_parameters as below. 我不明白为什么它说“不允许的参数:坐标”,但是我使用了如下的rails strong_parameters。
Please let me know whats wrong with my code. 请让我知道我的代码有什么问题。
I also read about the geospatial data storage in MongoDB which seems great. 我还阅读了有关MongoDB中地理空间数据存储的信息,这似乎很棒。 I would really appricaite if anyone could guide me any reference or links on How to store geospatial data with embedded document. 如果有人可以指导我有关如何使用嵌入式文档存储地理空间数据的任何参考或链接,我将非常适合。 So taht instead of storing data like this I will use geospatioal data storage as later I will also be implementing an algorithm over the stored data. 因此,不是像这样存储数据,我将使用地理空间数据存储,因为稍后我还将对存储的数据实现一种算法。
I suppose this issue is only related with Strong_parameters, so It wouldnt be problem if i change it to mongos Geospatial after this resolved. 我想这个问题仅与Strong_parameters有关,因此如果在解决此问题后将其更改为mongos Geospatial,就不会有问题。
`Private def user_params `私有def user_params
params.require(:user).permit(:name,
:coords_attributes => [:lat,
:long,:alt])
#I also Tried this
params.require(:user).permit(:name, {:coords => [:lat, :long, :alt])
end `
But, both approach didnt work for me. 但是,两种方法都对我不起作用。 Below is rest of my code: 以下是我的其余代码:
User Controller 用户控制器
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
def index
@users = User.all
end
def show
@users = User.all
end
def new
@user = User.new
end
def edit
end
def create
@user = User.new(user_params)
puts '**********************************************'
puts user_params
puts '*****************************************'
respond_to do |format|
if @user.save
format.html { redirect_to @user, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: @user }
else
format.html { render :new }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end
def user_params
params.require(:user).permit(:name,
:coords_attributes => [:lat,
:long,
:alt])
end
end
User.rb User.rb
class User
include Mongoid::Document
include Mongoid::Attributes::Dynamic
field :name
embeds_many :coords, class_name: "User"
#embeds_many :locations
accepts_nested_attributes_for :coords
#accepts_nested_attributes_for :locations
end
Coord.rb Coord.rb
class Coord
include Mongoid::Document
field :lat
field :long
field :alt
embedded_in :user, :inverse_of => :coords
end
_form.html.erb _form.html.erb
<%= form_for(@user) do |f| %>
<% if @user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% @user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<h3>Co-ordinates</h3>
<%= f.fields_for :coord do |c| %>
<div class="field">
<%= c.label :Latitude %><br>
<%= c.text_field :lat %>
</div>
<div class="field">
<%= c.label :Longitude %><br>
<%= c.text_field :long %>
</div>
<div class="field">
<%= c.label :Altitude %><br>
<%= c.text_field :alt %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I'm not sure if your posting JSON. 我不确定您是否发布JSON。 This worked for me posting JSON on my issue. 这对我发布问题的JSON很有帮助。
You have: 你有:
params.require(:user).permit(:name,
:coords_attributes => [:lat,
:long,
:alt])
But needs to be: 但需要是:
params.require(:user).permit(:name, {:coords => [:lat, :long, :alt]})
Here's additional information on the issue. 这是有关此问题的其他信息。
https://github.com/rails/rails/blob/master/actionpack/lib/action_controller/metal/strong_parameters.rb#L246-L247 https://github.com/rails/rails/blob/master/actionpack/lib/action_controller/metal/strong_parameters.rb#L246-L247
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.