我做了一个嵌套的资源(如下)。虽然是在coffee_profile#放映视图,创建用于经由链路的一个特定简档(/coffee_profiles/:coffee_profile_id/recipes/new(.:format))的特定配方。
resources :coffee_profiles do
resources :recipes, only: [:new, :create, :show]
end
我想追上coffee_profile_id存在于URL为参照并不配方所属coffee_profile和隐藏在食谱中hidden_field#新形式,只是为了与其它参数传递,同时提交。 不过,我不知道如何获取该ID本身,hidden_field创建此PARAM,但它填充了提交coffee_profile_id后无:“”
这是来自recipes_controller.rb
def new
@recipe = Recipe.new
end
def create
@recipe = current_user.recipes.build(recipe_params)
if @recipe.save
flash[:success] = "Recipe created!"
redirect_to root_path
else
flash[:error] = @recipe.errors.inspect
render :new
end
end
这是配方#新的form_for:
<%= form_for(@recipe) do |f| %>
...
<% end %>
任何帮助表示赞赏,让我知道,如果你需要更多的信息/代码。
由于使用的是嵌套资源,不知何故coffee_profile必须存在。
其实没有必要来存储你的食谱的地方,因为你的食谱具有的has_many和belongs_to的关系。
你需要改变你的形式,如下所示:
<%= form_for([@coffee_profile, Recipe.new]) do |f| %>
<div class="recipes_form_item">
<%= f.label :coffee %>
<%= f.text_field :coffee %>
</div>
<div class="recipes_form_item">
<%= f.label :quantity %>
<%= f.number_field :quantity %>
</div>
<div class="recipes_form_item">
<%= f.label :method %>
<%= f.text_field :method %>
</div>
<div class="recipes_form_item">
<%= f.label :water_temperature %>
<%= f.number_field :water_temperature %>
</div>
<div class="recipes_form_item">
<%= f.label :water_amount %>
<%= f.number_field :water_amount %>
</div>
<div class="recipes_form_item">
<%= f.label :grind %>
<%= f.text_field :grind %>
</div>
<div class="recipes_form_item">
<%= f.label :aroma %>
<%= f.text_field :aroma %>
</div>
<div class="recipes_form_item">
<%= f.label :aroma_points %>
<%= f.number_field :aroma_points %>
</div>
<div class="recipes_form_item">
<%= f.label :taste %>
<%= f.text_field :taste %>
</div>
<div class="recipes_form_item">
<%= f.label :taste_points %>
<%= f.number_field :taste_points %>
</div>
<div class="recipes_form_item">
<%= f.label :body %>
<%= f.text_field :body %>
</div>
<div class="recipes_form_item">
<%= f.label :body_points %>
<%= f.number_field :body_points %>
</div>
<div class="recipes_form_item">
<%= f.label :astringency %>
<%= f.text_field :astringency %>
</div>
<div class="recipes_form_item">
<%= f.label :astringency_points %>
<%= f.number_field :astringency_points %>
</div>
<div class="recipes_form_item">
<%= f.label :brew_time %>
<%= f.number_field :brew_time %>
</div>
<div class="recipes_form_item">
<%= f.submit :Submit %>
</div>
<% end %>
现在,你的coffee_profile_controller内,加在你的新方法(或任何你渲染你的食谱如下形式:
@coffee_profile = CoffeeProfile.find(params[:id])
现在,你的recipe_controller里面里面添加创建:
@coffee_profile = CoffeeProfile.find(params[:id])
@recipe = @coffee_profile.recipes.create(recipe_params)
if @recipe.save
redirect_to root_path
end
也改变了新的方法,一切都像这样里面:
DEF新@coffee_profile = CoffeeProfile.find(PARAMS [:ID])@recipe = @ coffee_profile.recipe.new端
并为recipe_params,请确保您接受:coffee_profile_id
params.require(:recipe).permit(:id, your other values and :coffee_profile_id)
现在,当你去到控制台,你可以说:@recipe = Recipe.last
然后@recipe.coffee_profile
,它会给你所有的信息传回关于咖啡的轮廓。
问候!