获取嵌套资源 ID 形式 URL

Obtaining nested resource id form URL


我做了一个嵌套资源(如下)。在 coffee_profile#show 视图中,通过 link (/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 食谱属于哪个并隐藏在食谱中#new form in hidden_field 只是为了在提交时将其与其他参数一起传递。

但是,我不知道如何获取该 id 本身,hidden_field 创建了这个参数,但是在提交后它被填充为 nil 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


这是食谱#new form_for:

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

感谢任何帮助,如果您需要更多帮助,请告诉我info/code。

console log

因为您正在使用嵌套资源,所以 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 里面添加 inside 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(参数[:id]) @食谱=@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,这将为您返回有关咖啡配置文件的所有信息。

您好!