Hi,
I am developing an application using:
ruby '2.0.0'
sinatra '1.4.4'
data_mapper: '1.2.0'
I am working with the following models:
##################### FILE: models.rb
require 'dm-core'
require 'dm-migrations'
require 'dm-validations'
require 'dm-types'
require 'date'
require 'dm-timestamps'
require 'bcrypt'
require 'date'
require 'dm-serializer'
DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/development.db")
class Property
include DataMapper::Resource
property :id, Serial
property :address, String
property :postal_code, String
attr_reader :address, :postal_code
# Associations
belongs_to :country
belongs_to :state
belongs_to :city
# Validations
validates_length_of :address, :max => 75
validates_length_of :postal_code, :max => 15
end
class Country
include DataMapper::Resource
property :id, Serial
property :name, String, :required => true
attr_reader :name
# Validations
validates_uniqueness_of :name
end
class City
include DataMapper::Resource
property :id, Serial
property :name, String, :required => true
attr_reader :name
# Associations
belongs_to :state
end
class State
include DataMapper::Resource
property :id, Serial
property :name, String, :required => true
attr_reader :name
# Associations
belongs_to :country
end
##################### END FILE: models.rb
In my application file I have the following verb that perform the UPDATE:
##################### FILE: main.rb
put '/properties/:id' do
protected!
@country_id = params[:country_id]
@state_id = params[:state_id]
@city_id = params[:city_id]
@property = Property.get(params[:id])
if @property.update(params[:property])
flash[:notice] = trans('message.property.update')
redirect to("/properties/#{@
property.id}")
else
flash.now[:error] = @property.errors.full_messages * '</br>'
erb '/property/edit_property'.to_sym, { :layout => :'/layouts/application' }
end
end
##################### END FILE: main.rb
My problem is that when i change the selected Country, State or City value and request the update, @property.update(params[:property]) is not updating the new values on child associations. I´ve tried to change the name of the HTML SELECT on the erb to property[country_id], but no success:
##################### FILE: property_form.erb
<div class="form-group">
<label for="country"><%= trans('models.general.country') %></label>
<select class="form-control" id="countrySelect" name="country_id">
<%= get_select_options(Country.list_options, @country_id) %>
</select>
</div>
<div class="form-group">
<label for="state"><%= trans('models.general.state') %></label>
<select class="form-control" id="stateSelect" name="state_id">
</select>
</div>
<div class="form-group">
<label for="city"><%= trans('models.general.city') %></label>
<select class="form-control" id="citySelect" name="city_id">
</select>
</div>
<div class="form-group">
<label for="address"><%= trans('models.general.address') %></label>
<input class="form-control" type="text" name="property[address]" value="<%= @property.address %>">
</div>
<div class="form-group">
<label for="postal_code"><%= trans('models.general.postal_code') %></label>
<input class="form-control" type="text" name="property[postal_code]" value="<%= @property.postal_code %>">
</div>
<ul class="list-inline">
<li><a href="<%= @back_href %>"><%= trans('actions.back') %></a></li>
<li><button class="btn btn-default"><%= trans('actions.save') %></button></li>
</ul>
##################### END FILE: property_form.erb
Thenks in advance.