Note that there are some explanatory texts on larger screens.

plurals
  1. POaccessors and update_attributes
    primarykey
    data
    text
    <p>I have a model with a location attribute, it's a array of two elements; latitude and longitude. I define the accessors for the location like this</p> <pre><code>class Address include Mongoid::Document include Mongoid::Timestamps include Mongoid::Spacial::Document field :location, :type =&gt; Array, spacial: {lat: :latitude, lng: :longitude, return_array: true } #accessors for location def latitude location[0] end def latitude=( lat ) location[0] = latitude end def longitude location[1] end def longitude=( lng ) location[1] = lng end attr_accessible :location, :latitude, :longitude end </code></pre> <p>here is the controller code</p> <pre><code>def create @address = Address.new(params[:address]) if @address.save redirect_to :action =&gt; 'index' else render :action =&gt; 'new' end end def update @address = Address.find(params[:id]) if @address.update_attributes(params[:address]) redirect_to :action =&gt; 'index' else render :action =&gt; 'edit' end end </code></pre> <p>and at the view level</p> <pre><code>&lt;%= f.hidden_field :latitude%&gt; &lt;%= f.hidden_field :longitude%&gt; </code></pre> <p>these hidden field are manipulated via js, and that is ok. I saw it view developers tools</p> <p>Here are the parameters the controller receives</p> <pre><code>"address"=&gt;{"latitude"=&gt;"-38.0112418", "longitude"=&gt;"-57.53713060000001", "city_id"=&gt;"504caba825ef893715000001", "street"=&gt;"alte. brown", "number"=&gt;"1234", "phone"=&gt;"223 4568965"}, "commit"=&gt;"Guardar", "id"=&gt;"504cacc825ef893715000006"} </code></pre> <p>Note that the latitude and longitude parameters changed and thats ok, but this change it's not saved to the mongodb</p> <p>So, The values for latitude and longitude are not saved. Is there any instruction my code is missing?</p> <p>Thanks in advance.</p> <p>--- edit ---</p> <p>here are the working accessors</p> <pre><code> def latitude location[0] end def latitude=( lat ) self.location = [lat,self.location[1]] end def longitude location[1] end def longitude=( lng ) self.location = [self.location[0], lng] end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload