سؤال

I am looking for a best practice on how to deal with this situation. I have a model called Fixture where I create fixtures for say a football match, after the game has finished I want to be able to edit that fixture and log the result for each team, but I am unsure on how to set my form up.

Ideally I want to be able to create a new fixture without the result parts showing and then when I edit the fixture the form will have those parts, so something like this:

New

 <div class="col-md-6 col-md-offset-3 well">
    <%= form_for @fixture, html: { multipart: true }  do |f| %>
    <fieldset>
    <!-- Form Name -->
    <legend>Create New Fixture</legend>
         <div class="form-group">
           <%= f.label :tournament_id  %>
           <%= f.collection_select :tournament_id, Tournament.all, :id, :name, { :prompt => "Please Select a Tournament" }, { class: 'form-control' } %>
         </div>

        <!-- Text input-->
        <div class="form-group">
          <%= f.label :home_team %>
          <%= f.text_field :home_team, class: 'form-control', placeholder: 'Home Team' %>
        </div>

       
        <div class="form-group">
          <%= f.label :away_team %>
          <%= f.text_field :away_team, class: 'form-control', placeholder: 'Away Team' %>
        </div>

        <div class="form-group">
          <%= f.label :kickoff_time %>
          <%= f.time_select :kickoff_time, class: 'form-control' %>
        </div>
        
        <div class="form-group">
          <%= f.submit 'Submit', :class => 'btn btn-success' %>
        </div>

      </fieldset>
    <% end %>
  </div>

Edit

 <div class="col-md-6 col-md-offset-3 well">
    <%= form_for @fixture, html: { multipart: true }  do |f| %>
    <fieldset>
    <!-- Form Name -->
    <legend>Edit Fixture</legend>
         <div class="form-group">
           <%= f.label :tournament_id  %>
           <%= f.collection_select :tournament_id, Tournament.all, :id, :name, { :prompt => "Please Select a Tournament" }, { class: 'form-control' } %>
         </div>

        <!-- Text input-->
        <div class="form-group">
          <%= f.label :home_team %>
          <%= f.text_field :home_team, class: 'form-control', placeholder: 'Home Team' %>
        </div>

       
        <div class="form-group">
          <%= f.label :away_team %>
          <%= f.text_field :away_team, class: 'form-control', placeholder: 'Away Team' %>
        </div>

        <div class="form-group">
          <%= f.label :kickoff_time %>
          <%= f.time_select :kickoff_time, class: 'form-control' %>
        </div>

        <div class="form-group">
          <%= f.label :home_team_result %>
          <%= f.text_field :home_team_result, class: 'form-control' %>
        </div>

        <div class="form-group">
          <%= f.label :away_team_result %>
          <%= f.time_select :away_team_result, class: 'form-control' %>
    </div>
        
        <div class="form-group">
          <%= f.submit 'Submit', :class => 'btn btn-success' %>
        </div>

      </fieldset>
    <% end %>
  </div>

At the moment when trying to access the edit form I am getting the error:

undefined method error :home_team_result

I am not sure if this has something to do with the fact that my column is set as :integer for that.

This is my schema:

create_table "fixtures", force: true do |t|
t.string   "home_team"
t.string   "away_team"
t.time     "kickoff_time"
t.integer  "tournament_id"
t.integer  "home_score_result"
t.integer  "away_score_result"
t.datetime "created_at"
t.datetime "updated_at"
end

A couple of ideas have crossed my mind:

  1. Hide the home_team_result and away_team_result on the new form and assign default values of 0

  2. Again hide the columns and have a button 'Add result' to reveal the fields

But I'm not sure that hiding the fields is the answer as that is open to manipulation. Ideally I would like to put some logic in that deals with this in a 'Rails Way', if that makes sense.

هل كانت مفيدة؟

المحلول

Looking at the error undefined method error :home_team_result, we can say that there is no field named home_team_result in Fixture model.

home_team_result must be an attribute in your Fixture model. Make it either a virtual attribute (using attr_accessor) or create a column in the fixtures table named home_team_result.

UPDATE

Actual column names in the table fixtures are home_score_result and away_score_result whereas in the view you are accessing home_team_result and away_team_result. Hence, the error.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top