无法使用 rails 将表单数据设置到数据库中
Not able to set form data into database using rails
我有一个 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_仪表板
<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
routes.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 请求,当您在表单上单击 'submit' 时将提交该请求到您的 ProjectManagersController。那里的代码看起来不错。
如果记录仍未创建,请检查您 运行 rails server
所在的终端中的日志。你能看到 POST 请求是从哪里进来的吗?遍历所有行,看看它们是否有意义。
我有一种感觉,项目站点没有分配给项目经理,这就是无法保存记录的原因。因为 project_manager.rb
中的 belongs_to :project_site
包含验证。是否有回滚而不是保存预项目管理器?
我有一个 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_仪表板
<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
routes.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 请求,当您在表单上单击 'submit' 时将提交该请求到您的 ProjectManagersController。那里的代码看起来不错。
如果记录仍未创建,请检查您 运行 rails server
所在的终端中的日志。你能看到 POST 请求是从哪里进来的吗?遍历所有行,看看它们是否有意义。
我有一种感觉,项目站点没有分配给项目经理,这就是无法保存记录的原因。因为 project_manager.rb
中的 belongs_to :project_site
包含验证。是否有回滚而不是保存预项目管理器?