- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 Bootstrap 模式,当我单击按钮时,将调用此 Bootstrap 模型(打开弹出菜单)并传递不同的offer_id。
此弹出菜单打开一个表单(如编辑表单),我编辑了所有信息,然后单击编辑按钮,然后仅更新第一条记录。
例如我想编辑offer_id = 1的信息,当填写所有表单并提交时,offer_id = 1会更新而不是offer_id = 5强>.
所以我不知道我到底错在哪里,以及我错过了什么..
同一页面上的编辑按钮和模型 (_employee_details.html.erb)
编辑按钮
<div class="edit-recr-wrp1">
<button type="button" class="btn btn-primary fa fa-pencil-square-o" data-toggle="modal" data-target="#exampleModal1" data-offer_id="<%= emp['offer_letter_id'] %>" data-employee_id="<%= emp['employee_id'] %>"></button>
</div>
模态“#exampleModal1”
<div class="modal fade" id="exampleModal1" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
</div>
<div class="modal-body">
<h2 class="text-center">Edit <span>Employee Details</span></h2>
<div class="post-new-job head-border">
<div class="alert alert-success" role="alert" id='success-job' style='display:none;'>Employee Details is successfully added.</div>
<div class="form-body">
<%= form_for :employee_details, :url =>{:controller => "hr", :action => "update" } do |f| %>
<div class="col-md-12">
<div class="mydata1">
<%= f.text_field :offer_letter_id, { class: 'form-control', id: 'offer_id-name' } %>
</div>
<div class="form-group">
<label class="control-label">Employee ID</label>
<div class="input-group"> <span class="input-group-addon"> <i class="fa fa-user"></i> </span>
<div class="mydata2">
<%= f.text_field :employee_id, { disabled: true, :required => true, placeholder: 'E12345678', class: 'form-control', id: 'employee_id-name' } %>
</div>
</div>
</div>
<div class="form-group">
<label class="control-label">Bank Account</label>
<div class="input-group"> <span class="input-group-addon"> <i class="fa fa-university"></i> </span>
<%= f.text_field :bank_ac, { :required => true, placeholder: '06464060852634865', class: 'form-control' } %>
</div>
</div>
<div class="form-group">
<label class="control-label">Bank IFSC Code</label>
<div class="input-group"> <span class="input-group-addon"> <i class="fa fa-code"></i> </span>
<%= f.text_field :bank_ifsc, { :required => true, placeholder: 'SBI012356', class: 'form-control' } %>
</div>
</div>
<div class="form-group">
<label class="control-label">End of Date</label>
<div class="input-group"> <span class="input-group-addon"> <i class="fa fa-calendar"></i> </span>
<%= f.text_field :work_end_date, { placeholder: 'MM/DD/YYYY', id: 'datepicker1', class:"datepicker_style" } %>
</div>
</div>
<div class="form-group">
<label class="control-label">Gender</label>
<div class="input-group"> <span class="input-group-addon"> <i class="fa fa-male fa-female"></i> </span>
<%= f.select :gender, ['Male', 'Female'], { :required => true }, class: "form-control" %>
</div>
</div>
<div class="form-group">
<label class="control-label">Spouse Name</label>
<div class="input-group"> <span class="input-group-addon"> <i class="fa fa-user"></i> </span>
<%= f.text_field :spouse_name, { :required => true, placeholder: 'Father/Mother/Wife name', class: "form-control" } %>
</div>
</div> <br>
<div class="form-group">
<a><%= f.submit "Edit Employee Details", :class => "btn btn-primary" %></a>
</div>
</div>
<div class="col-md-2"></div>
<%- end -%>
</div>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
// edit employee information
$('#exampleModal1').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget)
var offer_id = button.data('offer_id')
var employee_id = button.data('employee_id')
var modal = $(this)
modal.find('.mydata1 input').val(offer_id)
modal.find('.mydata2 input').val(employee_id)
});
</script>
<script type="text/javascript">
// edit employee information
$(document).ready(function () {
$('#datepicker1').datepicker({
format: "dd/mm/yyyy"
});
});
</script>
hr_controller.rb
类 HrController
def new
@employees = EmployeeDetail.new
end
# edit employee information
def edit
@employees = EmployeeDetail.find_by(params[:offer_letter_id])
end
def create
@employees = EmployeeDetail.new(employee_params)
if @employees.save
redirect_to :action => 'internal_employee_page'
else
redirect_to :action => 'internal_employee_page'
end
end
def show
@employees = EmployeeDetail.find(params[:id])
end
def update
@employees = EmployeeDetail.find_by(params[:offer_letter_id])
if @employees.update(employee_params)
redirect_to :action => 'internal_employee_page'
else
redirect_to :action => 'internal_employee_page'
end
end
private
def employee_params
params.require(:employee_details).permit(:offer_letter_id, :employee_id, :bank_ac, :bank_ifsc, :spouse_name, :gender, :work_end_date)
end
end
routes.rb
resources :hr
get 'hr/edit'
post 'hr/update'
最佳答案
您对编辑和更新方法有一些误解。你应该通过 id 找到,我的意思是它应该像
@employees = EmployeeDetail.find(id)
其次,请在更新方法中编写一个提升,以检查是否确实传递了数据或仅传递了第一个数据,这就是为什么它只更新了您的第一列。在默认更新=>
raise params.inspect
然后你将确定自己的参数是否正确,defemployee_params方法是否有效?
关于javascript - 如何将参数从 form_for 方法传递到 Rails 中的 Controller 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40394924/
是 {:action => 'update', :id => @product.id})) do |f| %> ... 和 {:action => 'update', :id => @pro
我实现了一个表单文本字段字符计数器,它可以在 jsfiddle 中工作,但不能在应用程序中工作。它会出现,但不会像在 jsfiddle 中那样实时计数。当我重新加载页面时,它将计算文本字段中的当前字符
我正在尝试创建一个只有一个文本字段和一个隐藏字段的表单,我将在将数据传递到 HTTP API 时使用该字段,因此我没有与之关联的模型,并且通过扩展,没有变更集。 是否可以做到这一点并使用 form_f
我的表单如下所示: ~~~form stuff~~~ { disable_with: "Processing" }, :class => "btn btn-info
我需要将表单提交到外部 URL,所以我有这个: form_for(@task, :url => "https://www.external.com/Submit") do |f|) @ass
我有两个模型: 品牌 has_many :products 产品 belongs_to :brand 在我看来 (show_brand.html.erb),我使用 @brand.name ... 显示
我在财务模块中有以下模型: class Finance::BillRec :destroy ... end class Finance::BillRecOff ... 路线.rb nam
我写了以下代码:- 翻译成 我的 routes.rb 文件包含以下代码:- root 'static_pages#home' get 'contact'=>'static_pages#conta
我正在试验嵌套资源: 我的路线: resources :conversations do resources :replies do resources :comments
我的项目是这样组织的:用户是主要资源,每个用户都有一个配置文件,每个配置文件都有一个位置,如下所示: resources :users do resource :profile, :c
我有一个表单,提交后会运行 sidekiq 工作程序。目前,如果您在空表单上单击“提交”,它会尝试运行工作程序。我想要的是它提醒用户在提交之前需要填写这些字段。 我尝试将 :required => t
我有一个关于 form_for 和嵌套资源的两部分问题。假设我正在编写一个博客引擎,并且我想将评论与文章相关联。我定义了一个嵌套资源,如下所示: map.resources :articles do
我想要一个 form_for @user,但要发布到用户 Controller 中的自定义操作。 我该怎么做? 最佳答案 form_for @user, :url => url_for(:contro
我有一个 Rails form_for html 元素。该表单有一个调用 JavaScript 函数的 onsubmit 属性。 onsubmit 工作正常,但是当我添加额外的参数时,js 函数不再受
我试图在提交之前验证我的 form_for 中所有必需的字段是否已完成,并且已完成 95%,但在检查和发出警报之前它仍然会提交。 这是表单的开头: {:id => "new_listing", :o
如果有更好的方式来表达这个问题,或者有更好的方式来做到这一点,请告诉我。 我想使用模态的 form_for 更新一些模型。用户单击“编辑”和带有字段的模式弹出窗口,然后可以单击提交或取消。我可以做到这
我正在尝试在 Rails 中使用客户端验证 Gem。我的表单一直在查找,直到将 `:validate => true"添加到我的 form_for 中。然后我收到一条错误消息,告诉我我编写它的方式是错
我在父 View 中有一个 div,它呈现 show 部分。在这个 show 部分中,我有一个按钮应该更改父部分的部分以呈现“form”部分以编辑对象,但我一直被卡住,因为似乎“form”部分的 fo
我正在使用 form_for 创建一个具有 1 个输入字段和 1 个按钮的表单。我希望提交按钮位于输入字段旁边而不是下方。 相关代码为: .input{border: 1px solid #66666
对于我下面的表格,如何防止表格跳到下一行?我查看了 Bootstrap 类“form-inline”,但似乎没有用,因为它禁用了我的表单。 Hello World! 这导致:
我是一名优秀的程序员,十分优秀!