我有一个project_site模型,用户在其中将名称文件上传到数据库中。 另一个模型是project_manager,它接受和拒绝布尔字段和备注字段以及每个project_site条目。 但我无法将备注和状态布尔数据设置到project_manager数据库中。这是我的代码:
project_manager.rb
class ProjectManager < ApplicationRecord
belongs_to :project_site
end
project_site.rb
class ProjectSite < ApplicationRecord
has_many :project_managers, dependent: :destroy
validates :name,:attendance, presence: true
end
project_manager_dashboard
<table>
<thead>
<tr>
<th>Uploaded By</th>
<th>Attendance File</th>
<th colspan="2"></th>
</tr>
</thead>
<tbody>
<% @project_sites.each do |project_site| %>
<tr>
<td><%= project_site.name.titleize %></td>
<% if project_site.attendance? %>
<td><%= link_to "View Attendance", project_site.attendance.url, :class => "fi-page-export-csv" %></td>
<% else %>
<td>No File Present</td>
<% end %>
<td>
<%= form_for [ @project_site, @project_manager ] do |f| %>
<div class="row">
<div class="medium-6 columns">
<%= f.radio_button :status, true %>
<%= f.label :approve %>
<%= f.radio_button :status, false %>
<%= f.label :reject %>
</div>
<br>
<br>
<div class="medium-6 cloumns">
<%= f.label :remark %><br/>
<%= f.text_area :remark %>
</div>
</div>
<div>
<%= f.submit 'Submit', :class => 'button primary' %>
</div>
<% end %>
</td>
<td><%= link_to 'Action', project_site, :class=>'button tiny primary' %></td>
</tr>
<% end %>
</tbody>
</table>
project_manager_controller.rb
class ProjectManagersController < ApplicationController
def index
@project_sites = ProjectSite.all.order("created_at DESC").paginate(page: params[:page], per_page: 10)
@project_manager = ProjectManager.new
#@project_manager.project_site_id = @project_site.id
end
def create
@project_manager = ProjectManager.new(remark_params)
@project_manager.project_site_id = params[:project_site_id]
@project_manager.save
redirect_to project_managers_path
end
def remark_params
params.require(:project_manager).permit(:remark, :status)
end
end
路线.rb
resources :project_sites
resources :project_managers
控制器端
class ProjectManagersController < ApplicationController
def index
@project_sites = ProjectSite.all.order("created_at DESC").paginate(page: params[:page], per_page: 10)
@project_manager = ProjectManager.new
end
def create
@project_manager = ProjectManager.new(remark_params)
@project_manager.save
redirect_to project_managers_path
end
def remark_params
params.require(:project_manager).permit(:remark, :status, :project_site_id)
end
end
视图侧
<tbody>
<% @project_sites.each do |project_site| %>
<tr>
<td><%= project_site.name.titleize %></td>
<% if project_site.attendance? %>
<td><%= link_to "View Attendance", project_site.attendance.url, :class => "fi-page-export-csv" %></td>
<% else %>
<td>No File Present</td>
<% end %>
<td>
<%= form_for @project_manager do |f| %>
<div class="row">
<div class="medium-6 columns">
<%= f.radio_button :status, true %>
<%= f.label :approve %>
<%= f.radio_button :status, false %>
<%= f.label :reject %>
</div>
<br>
<br>
<div class="medium-6 cloumns">
<%= f.label :remark %><br/>
<%= f.text_area :remark %>
</div>
<%=f.hidden_field :project_site_id, value: project_site.id%>
</div>
<div>
<%= f.submit 'Submit', :class => 'button primary' %>
</div>
<% end %>
</td>
<td><%= link_to 'Action', project_site, :class=>'button tiny primary' %></td>
</tr>
<% end %>
</tbody>
您创建的窗体是正确的,您需要一个嵌套窗体,因为您正在为项目网站创建项目经理。但是,如果窗体是嵌套的,则还需要嵌套路由。要创建新记录,它应如下所示:
routes.rb
resources :project_sites do
resources :project_managers, only: [:create]
end
(这称为浅嵌套,仅在创建新记录时嵌套(。这样,您就不必在隐藏字段中传递项目站点 ID,它将成为 url 的一部分。
现在,这应该将http请求路由到http请求,当您单击表单上的"提交"到您的项目经理控制器时,该请求将提交。那里的代码看起来不错。 如果仍未创建记录,请检查运行rails server
的终端中的日志。你能看到 POST 请求从哪里进来吗?浏览所有线条,看看它们是否有意义。 我有一种感觉,项目网站没有分配给项目经理,这就是无法保存记录的原因。因为project_manager.rb
中的belongs_to :project_site
包含验证。是否有回滚而不是保存预置管理器?