Rails: создайте форму для @score, в то время как в другой модели нет прямых ассоциаций - PullRequest
0 голосов
/ 07 марта 2012

Я хочу создать множественную форму для редактирования оценок из другой модели.

Основная модель - это модель Formrule, которая состоит из ассоциации habtm с моделью Scoretype и ассоциации habtm с моделью Room..

Обе модели используются для запроса модели Scores, в результате чего создается экземпляр @scores.Именно для этого экземпляра я хочу создать форму, но проблема в том, что поле field_for не создается.Я знаю, что @scores заполнены правильно, но форма не отображается.

Это та форма, которая у меня есть сейчас

<%= form_tag '/scores/update_scores' do %>
  <table>
    <tr>...</tr>
      <% for score in @scores %>
        <% fields_for :scores, score do |score| %>
           <tr>
              <td>
               <%= score.hidden_field(:form_id) %>
               <%= score.hidden_field(:team_id) %>
               <%= score.hidden_field(:scoretype_id) %>
            </td>
            <td>
              <%= score.number_field :scorevalue %>
            </td>
         </tr>
      <% end %>
    <% end %>
  </table>
  <%= submit_tag 'Update' %>
<% end %>

И это модели: Formrule

class Formrule < ActiveRecord::Base
  belongs_to :form
  has_and_belongs_to_many :scoretypes
  has_and_belongs_to_many :rooms
  has_many :teams, :through => :rooms
end

Тип счета

class Scoretype < ActiveRecord::Base
  has_many :scores
  has_and_belongs_to_many :formrules
end

Комната

class Room < ActiveRecord::Base
  has_many :teams
  has_and_belongs_to_many :formrules
end

Команда

class Team < ActiveRecord::Base
  has_many :scores
  belongs_to :room
  belongs_to :group
end

Счет

class Score < ActiveRecord::Base
  belongs_to :form
  belongs_to :team
  belongs_to :scoretype
  validates_uniqueness_of :id, :scope => [:team, :scoretype]
end

И наконец, использованный контроллер (Formrule)

  def show
    @formrule = Formrule.find(params[:id])
    @scoretypes = @formrule.scoretypes.all.collect
    @rooms = @formrule.rooms.all.collect
    @teams = Team.find(:all, :conditions => {:room_id => @rooms})
    @scores = Score.order("team_id").all(:conditions => {:scoretype_id => @scoretypes, :team_id => @teams})
...

end

Почему форма не отображается?есть предложения?

Спасибо всем заранее!

1 Ответ

0 голосов
/ 07 марта 2012

Попробуйте использовать <%= fields_for ... %> вместо <% fields_for ...%>.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...