Currently I'm creating something like a "sports-managing-and-results-gathering-app" in Rails 3. In this App I need to create several exercises which itself can have multiple 'resulttypes' (heartrate, distance in km, repititions, ...). And it should be possible to arrange the resulttypes in my prefered order. So, this is a classical many-to-many relationship.
I came up with the following migrations:
class CreateExercises < ActiveRecord::Migration
def self.up
create_table :exercises do |t|
t.integer :user_id
t.string :name
t.text :beschreibung
t.integer :resulttype_id
t.boolean :active, :default => true
t.timestamps
end
end
def self.down
drop_table :exercises
end
end
class CreateResulttypes < ActiveRecord::Migration
def self.up
create_table :resulttypes do |t|
t.string :name
t.string :einheit
t.text :beschreibung
t.timestamps
end
end
def self.down
drop_table :resulttypes
end
end
class CreateExercisesResulttypesJoin < ActiveRecord::Migration
def self.up
create_table :exercises_resulttypes, :id => false do |t|
t.integer "exercise_id"
t.integer "resulttype_id"
end
add_index :exercises_resulttypes, ["exercise_id", "resulttype_id"]
end
def self.down
drop_table :exercises_resulttypes
end
end
which works nicely with this formtastic code:
<%= semantic_form_for(@exercise) do |f| %>
<%= f.inputs do %>
<%= f.input :name %>
<%= f.input :beschreibung %>
<%= f.input :resulttype %>
<%= f.input :active %>
<% end %>
<%= f.buttons %>
<% end %>
The problem is here, that with this code I can not sort the resulttypes and each exercise can have each resulttype once only. So I changed the formtastic :resulttype form code to this (except the jquery code):
<div id="conn_ctrl" class="float_left center">
<div class="float_left center">
<%= f.select :resulttypes, @resall.collect{|d| [d.name,d.id]}.sort, {}, { :multiple => true, :class => "conn_select" } %><br>
</div>
<div class="float_left center">
<%= f.select :resulttypes, '', {}, { :multiple => true, :id => "exercise_resulttypes_save", :class => "conn_select" } %><br>
</div>
<div style="clear: both"></div>
<%= tag("input", { :type => "button", :id => "remove", :class => "dualbuttons", :name => "", :value => "<<" }, false) %>
<%= tag("input", { :type => "button", :id => "add", :class => "dualbuttons", :name => "", :value => 开发者_如何学Python">>" }, false) %>
<%= tag("input", { :type => "button", :id => "up", :class => "dualbuttons", :name => "", :value => "Up" }, false) %>
<%= tag("input", { :type => "button", :id => "down", :class => "dualbuttons", :name => "", :value => "Down" }, false) %><br>
</div>
<div style="clear: both"></div>
Now I have a "Dual Listbox" and I can add exercises to this specific workout multiple times and order it how I like it. BUT, when I submit the form, I get this error and I have no idea what that means:
Resulttype(#-614051528) expected, got String(#-608366078)
The submitted parameters are:
{"commit"=>"Create Exercise",
"authenticity_token"=>"v1l9zfxdxIJbdjZx6SsZ5tGuKMrlioBg+C9orSmVarA=",
"utf8"=>"✓",
"exercise"=>{"name"=>"dddd",
"beschreibung"=>"ddddd",
"resulttypes"=>["5",
"3",
"1"],
"active"=>"1"}}
This currently drives me crazy a little bit. I hope you know why this happens...
Rails expect an array of objects for the method association=
, use association_ids=
instead:
<%= f.select :resulttypes_ids, .... %>
精彩评论