A recipe
имеет много meal_plans
.У меня есть форма, которая пытается создать meal_plan
для recipe
, который я сейчас просматриваю.У меня есть два атрибута, которые я хотел бы установить для этого meal_plan
со скрытыми полями.Значения устанавливаются в DOM правильно, но они не сохраняются в базе данных.
recipes / show.html.erb
<%= form_for [@recipe, MealPlan.new] do |f| %>
<%= f.hidden_field :status, value: "Upcoming" %>
<%= f.hidden_field :user_id, value: current_user["uid"] %>
<%= f.submit "Create Meal Plan" %>
<% end %>
food_plan.rb
class MealPlan < ApplicationRecord
belongs_to :recipe
end
recipe_rb
class Recipe < ApplicationRecord
has_many :meal_plans, :dependent => :destroy
accepts_nested_attributes_for :meal_plans
...
end
recipes_controller.rb
...
def recipe_params
params.require(:recipe).permit(
:name, :link, :ingredients, :image_url,
meal_plans_params: [:recipe_id, :user_id, :status]
)
end
meal_plans_controller.rb
class MealPlansController < ApplicationController
def create
@recipe = Recipe.find(params[:recipe_id])
@meal_plan = @recipe.meal_plans.create!(params[:meal_plan_params])
redirect_to @recipe
end
end
Пример Результат
<MealPlan id: 10, recipe_id: 1, user_id: nil, status: nil, created_at: "2018-05-26 21:51:11", updated_at: "2018-05-26 21:51:11">
Скрытые поля для user_id
и status
имеют значения, когдаЯ смотрю на эти поля в инспекторе.Я не могу понять, почему эти поля не сохраняются.
Заранее спасибо!