
Rails3, Datamapper and select
Reported by Sylvain Desbureaux | April 6th, 2010 @ 03:55 AM | in 1.0.0
I'm testing rails3 with Datamapper on a simple project.
I've 2 classes :
@@@class Societe include DataMapper::Resource
property :id, Serial property :nom, String property :adresse, Text
has n, :clients end
@@@class Client
include DataMapper::Resource
property :id, Serial
property :prenom, String
property :nom, String
property :telephone, String
property :courriel, String
property :adresse, Text
property :view_only, Boolean
belongs_to :societe
end
I created them using rails generate scaffold (except belongs/
has of
course).
I've then have in the folder "view" 5 erb templates per class,
and
especially one (form) used by "new" and "edit" templates.
In order to select the society, I have added in client's form
a
select:
@@@<%= form_for(@client) do |f| %> <%= f.error_messages %> ...
<%= f.label :societe %><br />
<%= f.select(:societe_id, @societes.map{|s| [s.nom, s.id]}) %>
<%= f.submit %>
In creation mode, everything is allright and the customer is created with the link to its society But when I edit, I have the following error:
societe_id' for #<Client:0xa51833c>
@@@ Showing /home/.../app/views/clients/_form.html.erb where line #30 raised:
undefined method
Extracted source (around line #30):
27: </div> 28: <div class="field"> 29: <%= f.label :societe %><br /> 30: <%= f.select(:societe_id, @societes.map{|s| [s.nom, s.id]}) %> 31: </div> 32: <div class="actions"> 33: <%= f.submit %>
I've found an (very) ugly way to solve it:
in client class, I've added:
@@@class Client include DataMapper::Resource
property :id, Serial property :prenom, String property :nom, String property :telephone, String property :courriel, String property :adresse, Text property :view_only, Boolean
belongs_to :societe
def societe_id
societe.id
end end
This is in order to solve the issue. But then I wasn't able to update the record (the creation is still working well). So I've added some code in the controller:
@@@and in the update method of class_controller, I've added:
class ClientsController < ApplicationController # PUT /clients/1
# PUT /clients/1.xml
def update
@client = Client.get(params[:id])
#########################
<New Stuff>
id = params[:client][:societe_id].to_i
params[:client].delete(:societe_id)
#########################
</New Stuff>
respond_to do |format|
if @client.update(params[:client])
#########################
<New Stuff>
if id != @client.societe.id
@client.societe = nil
new_societe = Societe.get(id)
new_societe.clients << @client
@client.save
end
#########################
</New Stuff>
format.html { redirect_to(@client, :notice => 'Client was
successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @client.errors, :status
=> :unprocessable_entity }
end
end
end
end
As I said it's ugly but it works. I think that dm-rails should handle that in a better way...
Comments and changes to this ticket
-
Sylvain Desbureaux April 6th, 2010 @ 03:56 AM
Sorry for the formating, I must have forgot a @ @ @...
-
Martin Gamsjaeger (snusnu) April 11th, 2010 @ 10:55 AM
- State changed from new to unconfirmed
- Assigned user set to Martin Gamsjaeger (snusnu)
- Milestone set to 1.0.0
-
Martin Gamsjaeger (snusnu) June 3rd, 2010 @ 08:35 AM
- State changed from unconfirmed to resolved
If I understood correctly, I think that might be resolved with the addition of
DataMapper.finalize!
in the latest 1.0.0.rc3. Marking this resolved for now, feel free to reopen if that's actually not true!
Please Sign in or create a free account to add a new ticket.
With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »