获取嵌套的资源ID表单URL

时间:2019-02-12 08:55:08

标签: ruby-on-rails forms routing nested-resources


我做了一个嵌套的资源(如下)。在coffee_profile#show视图上时,通过链接(/coffee_profiles/:coffee_profile_id/recipes/new(.:format))为该特定配置文件创建特定配方。

resources :coffee_profiles do 
  resources :recipes, only: [:new, :create, :show]
end

我想捕获URL中存在的coffee_profile_id,以引用该食谱所属的coffee_profile,并隐藏在hidden_​​field的食谱#new表单中,以便在提交时将其与其他参数一起传递。

但是,我不知道如何获取该ID本身,hidden_​​field会创建此参数,但是在提交coffee_profile_id之后,它将填充为nil:


这是来自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


这是食谱#new form_for:

<%= form_for(@recipe) do |f| %>
  ...
<% end %>

感谢您的帮助,如果您需要更多信息/代码,请告诉我。

console log

1 个答案:

答案 0 :(得分:0)

由于您使用的是嵌套资源,因此必须以某种方式显示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内部添加create:

@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 结束

,对于cipal_params, 确保您接受:coffee_profile_id

params.require(:recipe).permit(:id, your other values and :coffee_profile_id)

现在,当您进入控制台时,您可以说:@recipe = Recipe.last

然后是@recipe.coffee_profile,这将为您提供有关咖啡资料的所有信息。

问候!