希望有人可以在这里提出建议。我对Rails相当新,并探索Rails 4.0中的变化。我在Rails 4.0中构建了这个简单的食谱书应用程序。我有食谱的主要模型(名称,cook_time,oven_temp,说明等)。因为一些食谱可能有5种成分而其他食谱可能有20种,我想在一个带有has_many关联的单独模型中打破成分。所以食谱has_many成分和accepts_nested_attributes_for:成分。以下是模型:
recipe.rb
class Recipe < ActiveRecord::Base
belongs_to :user
belongs_to :category
has_many :ingredients, :dependent => :destroy
validates :name, presence: true, length: { maximum: 50 }
accepts_nested_attributes_for :ingredients,
:reject_if => lambda { |a| a[:name].blank? },
:allow_destroy => true
end
ingredient.rb
class Ingredient < ActiveRecord::Base
belongs_to :recipe
end
提醒:Rails 4.0不再使用attr_accessible,而是使用强params将赋值移动到控制器中。
recipes_controller.rb
class RecipesController < ApplicationController
before_action :find_recipe, only: [:show, :edit, :update, :destroy]
before_action :set_current_user, except: [:index, :show]
respond_to :html, :js
...
def edit
end
def update
if @recipe.update_attributes(recipe_params)
redirect_to @recipe
else
render :edit
end
end
...
def find_recipe
@recipe = Recipe.find(params[:id])
end
private
def recipe_params
params.require(:recipe).permit( :id, :name, :category_id, :cook_time, :oven_temp, :calories, :instructions, :notes, :email, ingredients_attributes: [:id, :name])
end
end
我正在使用nathanvda出色的Cocoon gem动态管理嵌套表单字段,它在'recipes #new'中运行良好,并正确保存信息。但是,成分字段不会出现在“食谱#edit”视图中!这是我的'form'和ingredients_fields partials的代码。
_form.html.erb(缩写)
<%= form_for @recipe, html: { class: 'form-horizontal' } do |f| %>
<fieldset>
<legend>Main Information</legend>
<div class="field form-group">
<%= f.label :name, "Recipe name", class: "col-lg-2 control-label" %>
<div class="col-lg-5">
<%= f.text_field :name, class: "form-control" %>
</div>
</div>
<div class="field form-group">
<%= f.label :cook_time, class: "col-lg-2 control-label" %>
<div class="col-lg-5">
<%= f.text_field :cook_time, class: "form-control" %>
</div>
</div>
...
</fieldset>
<legend>Ingredients</legend>
<p>Number of ingredients: <%= f.object.ingredients.count unless f.object.ingredients.nil? %></p>
<fieldset id="ingredients">
<% f.fields_for :ingredients do |builder| %>
<%= render 'ingredient_fields', :f => builder %>
<% end %>
<p class="links">
<%= link_to_add_association 'add ingredient', f, :ingredients, { class:"btn btn-primary" } %>
</p>
</fieldset>
<fieldset>
<legend>How to make it</legend>
<div class="field form-group">
<%= f.label :instructions, class: "col-lg-2 control-label" %>
<div class="col-lg-5">
<%= f.text_area :instructions, class: "form-control", rows: "7" %>
</div>
</div>
...
</div>
</fieldset>
<% end %>
_ingredients_fields.html.erb
<div class="nested-fields">
<div class="form-group">
<%= f.label :name, "Ingredient", class: "col-lg-2 control-label" %>
<div class="col-lg-5">
<%= f.text_field :name, class: "form-control" %>
</div>
<%= link_to_remove_association "remove", f %>
</div>
</div>
正如我所说,非常简单,删除了所有错误检查和消息,只是基础知识。关于我缺少什么的任何想法?我知道当我从节目视图加载编辑时,配方中有成分。提前感谢任何建议!
凯文
答案 0 :(得分:6)
确认。它就像'='符号一样简单 - 必须查看&lt;%f.fields_for ...%&gt;一千次,只是没有注意到我错过了添加&lt;%=。
答案 1 :(得分:0)
SELECT
a.id
b.id
FROM
table1 AS a
JOIN
table2 AS b
ON
a.id = b.id