如何更新嵌套表单 - Ruby on Rails

如何解决如何更新嵌套表单 - Ruby on Rails

我的配方控制器中的更新方法有问题,我无法创建新类别。类别是配方中的嵌套资源。这是我的更新方法

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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?