Rails options_from_collection_for_select抛出预期的对象,得到字符串错误

时间:2015-09-16 22:31:32

标签: ruby-on-rails ruby ruby-on-rails-3

我尝试使用表单来编辑"匹配"对象,特别是team1team2属性,它们是对Team个对象的引用。但我一直收到错误

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

我接错了吗?我在options from collection_for_select中传递了错误的值吗?

表单本身看起来很好,并正确显示已保存的团队。

模型/ team.rb:

class Team < ActiveRecord::Base
  has_many :matches
end

模型/ match.rb:

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

控制器/ 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

视图/匹配/ _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 %>

</div>

错误:

        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"}

1 个答案:

答案 0 :(得分:0)

好的,我把它排序了。我需要将<%= f.select :team1更改为<%= f.select :team1_id,依此类推(请参阅下面的调整后代码)。我还需要允许:team1_id作为控制器中的强大参数: -

  def match_params
    params.require(:match).permit(:name, :short_name, :date, :venue_id, :team1_id, :team2_id,..........)
  end

我的views/matches/_form.html.erb:现在看起来像这样: -

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

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

        <% if f.object.team1.nil? %>
          <%= f.select :team1_id, options_from_collection_for_select(@teams, 'id', 'name') %>
        <% else %>   
          <%= f.select :team1_id, 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.team2.nil? %>
          <%= f.select :team2_id, options_from_collection_for_select(@teams, 'id', 'name') %>
        <% else %>   
          <%= f.select :team2_id, options_from_collection_for_select(@teams, 'id', 'name', f.object.team2.id) %>
        <% end %>

      </div>

    </div>