Reputation: 53
I am trying to use embedded relationship in MongoDB by using Mongoid with Rails 4.1.6. 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.
This issue is:
I can add data to my database using IRB or 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 :
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
Unpermitted parameters: coord {"name"=>"Appuvaa4"}
I cant understand why it says Unpermitted parameters: coord, however I have used rails strong_parameters as below.
Please let me know whats wrong with my code.
I also read about the geospatial data storage in MongoDB which seems great. 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.
`Private 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
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
class Coord
include Mongoid::Document
field :lat
field :long
field :alt
embedded_in :user, :inverse_of => :coords
end
_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 %>
Upvotes: 0
Views: 1271
Reputation: 1048
I'm not sure if your posting JSON. This worked for me posting JSON on my issue.
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.
Upvotes: 1