我正在尝试创建一个DRY表单,我可以将其用作form_fields,用于不同视图中的其他嵌套表单。我有两个问题。
_form
部分没有提交按钮,而是在表单上放置此按钮。但是,单击它当前位置的提交按钮什么都不做?_form
只是为了看到它保存,我收到了uninitialized constant UsersController
的路由错误?我的代码:
的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>
uninitialized constant UsersController
答案 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
位于用户项目中。