带嵌套资源Rails的嵌套表单4 [英] Nested Form with Nested Resource Rails 4

查看:70
本文介绍了带嵌套资源Rails的嵌套表单4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Rails 4.0.3上的嵌套资源创建一个嵌套的form_for.但是,我遇到了一些问题,并花了很多时间.

I am trying to create a nested form_for with a nested resource on Rails 4.0.3 However I've come to some problems and have spent quite a lot of time on it.

问题是当我尝试提交表单时,值未保存在数据库中.它将直接通过labs#index

The problem is when I tried to submit the forms, the values are not save in my database. It will redirect straight through the labs#index

我已经如下定义了嵌套资源和labs_controller.

I have defined nested resources and labs_controller as follows.

route.rb

...    
resources :users do
  resources :labs
end

labs_controller.rb (部分)

class LabsController < ApplicationController
  load_and_authorize_resource

  def new
    @user = User.find(params[:user_id])
    @lab  = Lab.new
  end

  def create
    @lab = Lab.new(lab_params)
    if @lab.save
      flash[:notice] = "Successfully created lab."
      redirect_to user_labs_url
    else
      render :action => 'index'
    end     
  end

  private

    def lab_params
      params.require(:lab).permit(:title, :description, 
        stages_attributes: [:id, :lab_id, :description, 
          tasks_attributes: [:id, :stage_id, :detail, :instruction, :answer ]])
    end
end

我有3个模型,分别是LabStageTask,每个模型都是通过has_many关系链接的.我还使用CanCan来处理授权.

I have 3 models, Lab, Stage and Task and each one are linked through has_many relationship. I also use CanCan to handle the authorization.

lab.rb

class Lab < ActiveRecord::Base
  belongs_to :user
  has_many :stages
  resourcify

  validates :user_id, presence: true
  accepts_nested_attributes_for :stages
end

stage.rb

class Stage < ActiveRecord::Base
  belongs_to :lab
  has_many :tasks
  resourcify

  accepts_nested_attributes_for :tasks, allow_destroy: true
end

task.rb

class Task < ActiveRecord::Base
  belongs_to :stage
  resourcify

end

这是我的表单部分.

_form.html.erb

<%= form_for [@user, @lab] do |f| %>
  <div class="field">
    <%= f.label :title %>
    <%= f.text_field :title %>
    <%= f.label :description%>
    <%= f.text_area :description %>
  </div>

  <%= f.fields_for :stages do |builder| %>
    <%= render "labs/partials/stage_fields", f: builder %>
  <% end %>

  <div><%= f.submit "Submit", class: "btn btn-primary" %></div>
<% end %>

_stage_fields.html.erb

<fieldset>
  <%= f.label :description %>
  <%= f.text_area :description %>
</fieldset>

<%= f.fields_for :tasks do |builder| %>
  <%= render "labs/partials/task_fields", f:builder %>
<% end %>

_task_fields.html.erb

<fieldset>
  <%= f.label :detail %>
  <%= f.text_area :detail %>
  <%= f.label :instruction %>
  <%= f.text_area :instruction %>
  <%= f.label :answer %>
  <%= f.text_field :answer %>
</fieldset>

我遇到了几个问题,但没有一个对我有用.例如

I've gone through several questions and none of it is working for me. Eg.

  • form_for with nested resources
  • nested form nested resources

我还尝试在嵌套模型表单上关注Ryan Bates,但仍然没有运气.希望有人能帮助我.

I also try to follow Ryan Bates on Nested Model Form and still no luck. Hope someone could help me.

提前谢谢!

推荐答案

查看错误日志,可能是您的lab_params函数存在的问题.当您查看传入的参数时,要将其与允许选项进行匹配.然后查看 strong参数文档,以获取有关如何处理嵌套参数的指导.

Looking at the error logs it is probably a problem with your lab_params function. When you look at the Parameters that are passed in you want to match those with your permit options. Then check out the strong params docs for guidance on how to handle nested params.

   def lab_params
      params.require(:lab).permit(:title, :description,
        :stage => [:id, :lab_id, :description,
          :tasks => [:id, :stage_id, :detail, :instruction, :answer ]
        ]
      )
    end

我认为这应该使您摆脱该错误.

I think this should get you beyond that error.

这篇关于带嵌套资源Rails的嵌套表单4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆