mercredi 16 septembre 2015

Rails options_from_collection_for_select throwing object expected, got string error

I'm not sure what I'm doing wrong here. I'm trying to use a form to edit a Match object - specifically the team1 & team2 attributes which are references to Team objects. But I keep getting the error Team(#70077411807740) expected, got String(#18738520)

Have I wired it up wrong? Am I passing the wrong value in the options from collection_for_select?

The form itself looks fine and is correctly displaying the saved Team.

Thanks for looking

models/team.rb

class Team < ActiveRecord::Base
  has_many :matches
end

models/match.rb

class Match < ActiveRecord::Base
  belongs_to :team1, class_name: 'Team'
  belongs_to :team2, class_name: 'Team'   
end

controllers/matches_controller.rb

def update
  if !current_admin
    flash[:alert] = "You are not authorised for that action"
    redirect_to matches_path
  else  
    puts "The updated match parameters are:"
    puts match_params.inspect 
    @match.update(match_params)
    redirect_to edit_match_path(@match.id)
  end  
end

private

def match_params
  params.require(:match).permit(:round_id, :pool_id, :name, :short_name, :date, :venue_id, :team1, :team2, :knockout_desc1, :knockout_desc2, :team1_score, :team1_tries, :team1_points, :team2_score, :team2_tries, :team2_points, :winner, :tries, :margin_id, :played)
end

views/matches/_form.html.erb

<div class="container">

   <h1 class="text-center">Edit Match <%= @match.id %></h1>


    <%= form_for(@match) do |f| %>
      <% if @match.errors.any? %>
        <div id="error_explanation">
          <h2><%= pluralize(@match.errors.count, "error") %> prohibited this match from being saved:</h2>

          <ul>
          <% @match.errors.full_messages.each do |message| %>
            <li><%= message %></li>
          <% end %>
          </ul>
        </div>
      <% end %>

      <div class="form-section">

        <div class="row text-center bottom-buffer-small">

          <div class="col-sm-5">
            <span class="header-1">Team 1</span>
          </div>

          <div class="col-sm-2">

          </div>

          <div class="col-sm-5">
            <span class="header-1">Team 2</span>
          </div> 

        </div>   

        <div class="row text-center bottom-buffer-small">        

          <div class="col-sm-5">

            <% if f.object.team1.nil? %>
              <%= f.select :team1, options_from_collection_for_select(@teams, 'id', 'name') %>
            <% else %>   
              <%= f.select :team1, options_from_collection_for_select(@teams, 'id', 'name', f.object.team1.id) %>
            <% end %> 

          </div>

          <div class="col-sm-2">
            <b>v</b>
          </div>

          <div class="col-sm-5">

            <% if f.object.team1.nil? %>
              <%= f.select :team2, options_from_collection_for_select(@teams, 'id', 'name') %>
            <% else %>   
              <%= f.select :team2, options_from_collection_for_select(@teams, 'id', 'name', f.object.team2.id) %>
            <% end %>

          </div>

        </div>  

      </div>


      <div class="actions">
        <%= f.submit %>
      </div>
    <% end %>


  <%= link_to 'Show', @match %> |
  <%= link_to 'Back', matches_path %>

Error

        Team(#70077413116680) expected, got String(#18738520)

        Extracted source (around line #51):
        49
        50
        51
        52
        53
        54

              puts "The updated match parameters are:"
              puts match_params.inspect 
              @match.update(match_params)
              redirect_to edit_match_path(@match.id)
            end  
          end

        Rails.root: /home/alzer/workspace/pawchallenge

        Application Trace | Framework Trace | Full Trace
        app/controllers/matches_controller.rb:51:in `update'
        Request

        Parameters:

        {"utf8"=>"✓",
         "_method"=>"patch",
         "authenticity_token"=>"SwlPKcm0grrgfggghkjdjhgjhg00s7767hsjkjkAGhzGwE2GbByFUi39p7ZI=",
         "match"=>{"team1"=>"11",
         "team2"=>"12"},
         "commit"=>"Update Match",
         "id"=>"1"}

Aucun commentaire:

Enregistrer un commentaire