Rails为具有多对多关系的模型创建表单 [英] Rails create form for model with many to many relation
问题描述
我有两个具有has_and_belongs_to_many
关系的模型,Recipe
和Tag
.对于这种关系,我有一个简单的联接表RecipesTags
.
I have two models, Recipe
and Tag
, with a has_and_belongs_to_many
relation. For this relation I have a simple join table, RecipesTags
.
食谱:
has_and_belongs_to_many :tags
标签:
has_and_belongs_to_many :recipes
现在,在创建新食谱时,用户可以以复选框的形式填写食谱所属的类别,例如肉",鱼"等.这些类别实际上只是数据库中的标记.
Now upon creating a new recipe, the user gets to fill in which category the recipe belongs to in forms of checkboxes, like "Meat", "Fish", and so on. These categories are in fact just tags in the database.
问题:食谱没有保存任何标签.
配方new
和create
控制器方法:
def new
@recipe = Recipe.new
@ingredients = Ingredient.all
@tags = Tag.all
respond_to do |format|
format.html # new.html.erb
format.json { render json: @recipe }
end
end
# POST /recipes
# POST /recipes.json
def create
@recipe = Recipe.new(params[:recipe])
if (params[:tags])
@recipe.tags << params[:tags]
end
respond_to do |format|
if @recipe.save
format.html { redirect_to @recipe, notice: 'Recipe was successfully created.' }
format.json { render json: @recipe, status: :created, location: @recipe }
else
format.html { render action: "new" }
format.json { render json: @recipe.errors, status: :unprocessable_entity }
end
end
end
视图:
<%= form_for(@recipe, :html => {:multipart => true}) do |f| %>
<% if @recipe.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@recipe.errors.count, "error") %> prohibited this recipe from being saved:</h2>
# [ fields that get's saved for the recipe and works fine ]
<% @tags.each do |t| %>
<%= f.label t.name %>
<%= f.check_box :tags, t.name %>
<br />
<% end %>
<%= f.submit 'Submit recipe', :class => 'btn btn-primary' %>
<% end %>
此刻,我收到一条错误消息: 未定义的方法合并"为肉":字符串
At the moment, I get an error message saying: undefined method `merge' for "Meat":String
肉"是标签名称.
那么,我在这里做什么错了?
So, what am I doing wrong here?
推荐答案
我认为问题出在这行@recipe.tags << params[:tags]
.
您使用<<
调用的关联方法需要一个对象(在这种情况下,需要一个标签对象),但是在这种情况下,似乎您可能要向它传递一个字符串.
I think the issue is this line @recipe.tags << params[:tags]
.
The association method you're calling with <<
takes an object (in this case expecting a tag object), but in this case it seems you might be passing it a string.
有关更多信息,此链接可能有帮助. org/association_basics.html#has_and_belongs_to_many-association-reference ,尤其是在引用collection<<(object, …)
的地方.
For more info this link may be helpful http://guides.rubyonrails.org/association_basics.html#has_and_belongs_to_many-association-reference, in particular where it refers to collection<<(object, …)
.
在控制器中,您需要执行类似@recipe.tags << tag
的操作,其中tag是特定的标记对象.
In your controller you'll want to do something like @recipe.tags << tag
where tag is a specific tag object.
因此,请尝试以下操作:
So, try this:
在您的控制器中
params[:tags].each do |k,v|
@recipe.tags << Tag.find(k)
end
您认为
<% @tags.each do |t| %>
<%= f.label t.name %>
<%= f.check_box "tags[#{t.id}]" %>
<br />
<% end %>
这篇关于Rails为具有多对多关系的模型创建表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!