Rails DRY多个视图的嵌套表单

时间:2016-10-20 10:22:52

标签: ruby-on-rails nested dry

我正在尝试创建一个DRY表单,我可以将其用作form_fields,用于不同视图中的其他嵌套表单。我有两个问题。

  1. 我希望_form部分没有提交按钮,而是在表单上放置此按钮。但是,单击它当前位置的提交按钮什么都不做?
  2. 表单在当前状态下显示正常。如果我将提交按钮移到_form只是为了看到它保存,我收到了uninitialized constant UsersController的路由错误?
  3. 我的代码:

    的routes.rb

    devise_for :users
    
    resources :users do
      resources :projects
    end
    

    user.rb模型 - 我正在使用Devise。

    class User < ApplicationRecord
      devise :database_authenticatable, :registerable,
             :recoverable, :rememberable, :trackable
    
      validates :password, :presence => true,
                           :on => :create,
                           :format => {:with => /\A.*(?=.{8,})(?=.*\d)(?=.*[\@\#\$\%\^\&\+\=]).*\Z/ }
    
      has_many :projects, inverse_of: :user
      accepts_nested_attributes_for :projects
    
    end
    

    projects.rb模型

    class Project < ApplicationRecord
      belongs_to :user, inverse_of: :projects
      ...
    end
    

    projects_controller.rb

    class ProjectsController < ApplicationController
    
    ...
    
    def new
      @project = current_user.projects.build
    end
    
    def create
      @project = current_user.projects.new(project_params)
      if @project.save
        redirect_to root_path, notice: 'Your project is being reviewed. We will be in contact soon!'
      else
        render :new
      end
    end
    
    ...
    
    private
    
    ...
    
    def project_params
      params.require(:project)
            .permit(
              :user_id, :project_type_id, :name, :industry_id, 
              :description, :budget_id, :project_status_id, feature_ids:[], addon_ids:[]
            )
    end
    
    end
    

    _form.html.erb部分视图

    <%= form_for @project do |f| %>      
      <div class="project_form">
        <ol>
          <div class="field entry_box">
            <li><%= f.label "Give your project a name" %>
            <%= f.text_field :name, placeholder: "A short name", class: "form-control entry_field" %></li>
          </div>
    
          ...... # All of the other form fields
    
          <div class="field entry_box">
            <li><%= f.label "What budget do you have in mind?" %>
            <%= collection_select(:project, :budget_id, Budget.all, :id, :list_of_budgets, {include_blank: 'Please select'}, {class: "form-control entry_field"} ) %></li>
          </div>
        </ol>
        # No f.submit button -> moved to view
      </div>
    <% end %>
    

    新项目的new.html.erb视图

    <div class="container">
      <div class="center">
        <h1>New Project</h1>
      </div>
    
      <%= form_for current_user do |f| %>
        <%= f.fields_for @project do |builder| %>
          <%= render 'form', :locals =>  { f: builder } %>
        <% end %>
        <div class="actions center space_big">
          <%= f.submit "Save Project", class: "btn btn-lg btn-success" %>
        </div>
      <% end %>
    </div>
    
    1. 如何让提交按钮在当前位置工作?
    2. 导致uninitialized constant UsersController
    3. 的路由错误的原因是什么

1 个答案:

答案 0 :(得分:0)

_form部分中,您不需要第一行即form_for,因为您已经通过了f这是form ('builder')的{​​{1}}对象,因为您在projects区块内创建了它。

所以这么做会:

fields_for @project

并在<div class="project_form"> <ol> <div class="field entry_box"> <li><%= f.label "Give your project a name" %> <%= f.text_field :name, placeholder: "A short name", class: "form-control entry_field" %></li> </div> ...... # All of the other form fields <div class="field entry_box"> <li><%= f.label "What budget do you have in mind?" %> <%= collection_select(:project, :budget_id, Budget.all, :id, :list_of_budgets, {include_blank: 'Please select'}, {class: "form-control entry_field"} ) %></li> </div> </ol> # No f.submit button -> moved to view </div> 更改form_for current_user行中添加如下内容:

render

并且你得到的是<%= render partial: 'form', :locals => { f: builder } %> ,因为你没有controller error并且UsersController正在nested_form User所以你应该拥有current_user此代码位于UsersController&#39; new操作中,build位于用户项目中。