Rails模型错误AssociationTypeMismatch期望,得到String

[英]Rails model error AssociationTypeMismatch expected, got String


I'm attempting to create a Rails 3 app with three models, with has_many and belongs_to relationships. I'm getting an AssociationTypeMismatch error when I try to submit a form. Specifically Location(#70232625418240) expected, got String(#70232609906560) The models I've set up are:

我正在尝试使用has_many和belongs_to关系创建一个包含三个模型的Rails 3应用程序。我尝试提交表单时收到了AssociationTypeMismatch错误。具体位置(#70232625418240)预计,得到字符串(#70232609906560)我设置的模型是:

  • Food
  • Location
  • Month

Right now I'm working on connecting the Food and Location models. I've created a simple form form @food which pulls in the entries from @location as checkboxes. I'd like to select different locations by checking them off, and associate them with the locations index in my Food model.

现在我正在努力连接Food和Location模型。我创建了一个简单的表单形式@food,它将@location中的条目作为复选框拉入。我想通过检查它们来选择不同的位置,并将它们与我的食物模型中的位置索引相关联。

I'm trying to have the checkboxes submit as an array, so I can associate the locations with the foods. So I'm submitting the location ids, which I believe will allow me to pull in the location Name for each one associated with a food entry.

我正在尝试将复选框提交为数组,因此我可以将位置与食物相关联。所以我提交了位置ID,我相信这将允许我为每个与食物条目相关的位置输入名称。

My models are set up this way:

我的模型设置方式如下:

food.rb

class Food < ActiveRecord::Base
  attr_accessible :area, :description, :icon, :iconSource, :image, :locations, :months, :name, :source, :type, :month_id
  has_many :locations
  has_many :months
end

location.rb

class Location < ActiveRecord::Base
  attr_accessible :city, :region, :regionName, :state, :title, :food_id, :month_id, :locations_id
  has_many :months
  belongs_to :foods
end

The form I have set up looks like this:

我设置的表单如下所示:

new.html.erb

<%= form_for @food do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
    <div class="field">
        <%= f.text_field :name, placeholder: "Name", :class => 'field-name' %>
    </div>

    <% for location in Location.find(:all) %>
        <div>
          <%= check_box_tag "food[locations_id][]", location.id %>
          <%= location.title %>
        </div>
    <% end %>

    <%= f.submit "Post" %>
<% end %>

The Foods controller is using the create action to process the form:

Foods控制器正在使用create动作来处理表单:

foods_controller.rb

class FoodsController < ApplicationController
  def index
    @foods = Food.all
  end
  def new
    @food = Food.new
    @locations = Location.all
  end
  def create
    @food = Food.new(params[:food])
    if @food.save
        redirect_to foods_url(@food), :notice => "Food created!"
    else
      render :action => "new"
    end
  end
end

The form submits just fine when I'm only entering data relating to the Foods model. But when I select one the of Location checkboxes and try to submit the form, I'm getting this Location(#70232625418240) expected, got String(#70232609906560) error. Here's the rest of the output:

当我只输入与Foods模型相关的数据时,表单提交正常。但是,当我选择一个位置复选框并尝试提交表单时,我得到这个位置(#70232625418240)预期,得到字符串(#70232609906560)错误。这是输出的其余部分:

app/controllers/foods_controller.rb:10:in `new'
app/controllers/foods_controller.rb:10:in `create'

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"QThXhDG8pPJcRVTTW6FXmo6MhGcoUeUspBhRtrbsbig=",
 "food"=>{"name"=>"Apple",
 "locations"=>["1"]},
 "commit"=>"Post"}

After looking into this for a few hours, I have tried everything I could find to fix this. I realize that I need to be using the location_id instead of locations. But after that, I'm turning up empty on other solutions here. Has anyone had a similar issue?

在调查了几个小时之后,我已经尝试了一切可以解决的问题。我意识到我需要使用location_id而不是位置。但在那之后,我在其他解决方案上空了。有没有人有类似的问题?

2 个解决方案

#1


2  

Use location_ids instead of locations_id:

使用location_ids而不是locations_id:

<%= check_box_tag food[location_ids], location.id -%>

Please also read the answers to rails 3 has_many :through Form with checkboxes.

另请阅读rails 3 has_many的回答:通过表单和复选框。

Edit:

:location_id to food[location_ids]

:location_id到食物[location_ids]

and add this location_id in to the attr_accessible.

并将此location_id添加到attr_accessible。

#2


1  

When I had this problem I solves it by using accepts_nested_attributes_for.

当我遇到这个问题时,我使用accepts_nested_attributes_for解决了这个问题。

class Food < ActiveRecord::Base
  attr_accessible :area, :description, :icon, :iconSource, :image, :locations, :months,  :name, :source, :type, :month_id
  has_many :locations
  has_many :months

  accepts_nested_attributes_for :locations, :allow_destroy => :true

  end

You can refer to this video which will explain better than me what you need to do in view and else.

你可以参考这个视频,它将比你更好地解释你在视野中需要做什么。

http://railscasts.com/episodes/196-nested-model-form-part-1


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2013/08/28/2e067dc7e07127dcddf8ae090f990922.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号