Thursday, 15 March 2012

relationship - rails database query, html drop down, interaction 1:n relation -



relationship - rails database query, html drop down, interaction 1:n relation -

good eve guys,

i have generated 4 scaffolds person, trainer, sportsman , index.

class person < activerecord::base attr_accessible :alter, :name end class sportler < person belongs_to :trainer end class trainer < person has_many :sportler end

then have created 1 form: views/index/index.html.erb

<table> <tr> <td>trainer</td> <td> trainers = trainer.find(:all) <% @trainers.each |trainer| %> puts "print results in <select> options" <select name="trainer"> </option>max musterman</option> </select> <% end %> </td> <td> <%= link_to 'neuer trainer', new_trainer_path %> </td> <tr> <tr> <td>sportler</td> <td> <!-- <% @sportlers.each |sportler| %> <select name="sportler"> </select> <% end %> --> </td> <td> <%= link_to 'neuer sportler', new_sportler_path %> </td> <tr> <tr> <td></td> <td></td> <td><input type="button" name="trspadd" value="hinzufügen"></td> <tr> </table>

now want add together trainer , sportman dropdown's. *one trainer can have 1 or more sportman. 1 sportsman can have 1 trainer. have considered when using form. how can solve this.*

my origin this, doesnet query trainer rows , set drop down:

trainers = trainer.find(:all) <% @trainers.each |trainer| %> puts "print results in <select> options" <select name="trainer"> </option>max musterman</option> </select> <% end %>

you should utilize select_tag formtaghelper, this:

select_tag "trainer", options_from_collection_for_select(@trainers, "id", "name")

ruby-on-rails relationship

No comments:

Post a Comment