嵌套表单不保存

时间:2017-10-08 17:53:05

标签: ruby-on-rails simple-form ruby-on-rails-5.1 nested-form-for

我是新的做rails和MVC。我正在尝试将一个嵌套的表单保存到我的数据库中,我使用simple-form和嵌套表单为gems。 当我提交表单时,只创建并保存“父”模型。 非常感谢你。

'父模型':

class DietCompose < ApplicationRecord
    belongs_to :diet
    belongs_to :food
end

'孩子'模特:

class DietsController < ApplicationController
    def new
        @diet = Diet.new
        @diet.diet_composes.build
    end

    def create
        @diet = Diet.new(diet_params)
        if @diet.save
            flash[:success] = "success"
        end
    end
    def diet_params
      params.require(:diet).permit(:name, :coach_id, :diet_composes_attributes)
    end

end

'父'控制器:

class DietComposesController < ApplicationController
    def new
        @diet_compose = Diet_compose.new
    end

    def create
        @diet_compose = Diet_compose.new(diet_compose_params)
        if @diet_compose.save
            flash[:success] = "success"
        end
    end

    def diet_compose_params
      params.require(:diet_compose).permit(:quantity, :hour, :day, :food_id, :diet_id)
    end
end

'孩子'控制器:

<%= simple_form_for @diet, :html => {:class => 'form-basic' }  do |f| %>
            <%= f.input :name %>
            <%= f.input :coach_id %>
            <%= f.nested_fields_for :diet_composes do |ff| %>
                    <%= ff.remove_nested_fields_link %>
                    <%= ff.input :hour %>
                    <%= ff.input :day %>
                    <%= ff.input :food_id %>
                    <%= ff.input :diet_id %>
                <% end %>
            <%= f.add_nested_fields_link :diet_composes %>
            <%= f.button :submit %>
        <% end %>

表单视图:

{{1}}

另外,当我执行命令时

  

Diet_compose.all

在rails控制台上我收到了错误

  

LoadError:无法自动加载常量Diet_compose,预期   /home/tanaka/Desktop/E-xercite/app/models/diet_compose.rb来定义它     来自(irb):8

1 个答案:

答案 0 :(得分:0)

你需要在这里纠正多件事。我已在内联添加评论。

Diet模型中的

diet.rb

class Diet < ApplicationRecord
  attr_accessible :diet_composes # make the attributes accessible for mass assignment.
end

在控制器中:

class DietsController < ApplicationController
    def new
        @diet = Diet.new
        @diet.diet_composes.build
    end

    def create
        @diet = Diet.new(diet_params)
        if @diet.save
            flash[:success] = "success"
        end
    end
    def diet_params
       # add attributes of nested association to whitelist
      params.require(:diet).permit(:name, :coach_id, :diet_composes_attributes => [:hour, :day, :food_id, :diet_id])
    end

end

您并非真的需要DietComposesController的操作才能使Diet的嵌套属性正常工作。但是,仍然要更正下面给出LoadError

的错误
class DietComposesController < ApplicationController
    def new
        @diet_compose = DietCompose.new # Model name is always CamelCase. DietCompose rather than Diet_compose
    end

    def create
        @diet_compose = DietCompose.new(diet_compose_params) # here as well
        if @diet_compose.save
            flash[:success] = "success"
        end
    end

    def diet_compose_params
      params.require(:diet_compose).permit(:quantity, :hour, :day, :food_id, :diet_id, )
    end
end

您的命令应为DietCompose.all。甚至看看diet_model.rb的定义。它将名称声明为DietModel

您的观点似乎很好。我还没有和nested_form_fields宝石一起工作,所以不能100%肯定。简单的调试规则,请参阅rails服务器的日志并推断:)