问题描述
我的配方控制器中的更新方法有问题,我无法创建新类别。类别是配方中的嵌套资源。这是我的更新方法
def update
if current_user.id != @recipe.category.user_id
flash.Now[:notice] = "You cannot update a recipe that you did not author."
redirect_to recipes_path
else
@recipe.update(recipe_params)
flash.Now[:notice] = "#{@recipe.title} has been updated."
redirect_to recipe_path
end
if @category
recipe.build_category
end
end
我最近刚刚添加了 if @category recipe.build_category
,但没有任何作用。
这是我的食谱强参数:
def recipe_params
params.require(:recipe).permit(
:title,:description,:category_id,category_attributes: [:name],instructions_attributes: [:id,:step,:_destroy
]
)
end
还有我在操作前的食谱控制器:
before_action :redirect_if_not_logged_in
before_action :find_recipe,only: [:show,:edit,:update,:destroy]
before_action :find_category,only: [:index,:new,:create]
我的食谱中也有这种语言_form:
<%= f.fields_for :category,recipe.build_category do |cb| %>
<div style="margin-left:30px">
<%= cb.label :name %>
<%= cb.text_field :name %>
</div>
<% end %>
然后这个在我的食谱中 edit.html.erb
<%= render partial: "form",locals: {recipe: @recipe,category: @category,button_name: "Update Recipe"}%>
这些是我的食谱和分类路线:
resources :recipes,:show,:new]
end
resources :recipes do
resources :categories,:create]
end
resources :users,:new] do
resources :recipes,only: [:index]
end
这是我的食谱模型:
belongs_to :category
accepts_nested_attributes_for :category,reject_if: :all_blank
has_many :instructions,dependent: :destroy
accepts_nested_attributes_for :instructions,reject_if: :all_blank,allow_destroy: true
validates :title,presence: true
validates :instructions,presence: true
validates :description,presence: true
end
有谁知道为什么我不能create
现有食谱中的类别?
**旁注 - 我的闪存错误也不起作用。在此先感谢您的帮助。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)