#1233 ✓resolved
Sylvain Desbureaux

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 %>
<% end %>

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:


@@@ Showing /home/.../app/views/clients/_form.html.erb where line #30 raised:


undefined method
societe_id' for #&lt;Client:0xa51833c&gt;
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 &lt;&lt; @client
      @client.save
    end



#########################

</New Stuff>



    format.html { redirect_to(@client, :notice =&gt; 'Client was



successfully updated.') }
    format.xml  { head :ok }
  else
    format.html { render :action =&gt; &quot;edit&quot; }
    format.xml  { render :xml =&gt; @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

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.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Pages