如何在不提交表单的情况下将值传递给Rails模型方法? [英] how to pass a value to rails model method without submit form?
本文介绍了如何在不提交表单的情况下将值传递给Rails模型方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的模型中有一个如下所示的类:
I have a class in my model like the following:
class Project::Area < ActiveRecord::Base
attr_accessor :cancel_id
def save_loc_values
update_record = Project::Loc.find_by_Project_Reference(project.Project_Reference)
update_record.update_attributes({
Status: 'D',
Cancellation_Date: DateTime.now().strftime("%Y-%m-%d %T.%L"),
CancellationReason_id: self.cancel_id
})
end
end
在我的form
中,我正在使用attr_accessor
字段
In my form
, am using the attr_accessor
field
<%= form_for @area, remote: true, html: {data: {save_warning_form: true}} do |f| %>
<%= form_error(f) %>
<%= f.text_field :cancel_id %>
<%= link_to 'omit', save_loc_values_project_areas_path(area_id: @area.id, cancel_id: @area.cancel_id), remote: true, method: :post %>
<% end %>
我在controller
中创建了一条路线并为我的自定义方法,如下所示,
I created a route and for my custom method in my controller
like the following,
def save_loc_values
@area = Project::Area.find(params[:area_id])
@return = @area.save_loc_values
end
它的问题运行正常,并将Status
和Cancellation_Date
的值保存到另一个数据库表中.但是On change cancel_id值没有保存.如何通过并保存onchange cancel_id
?
The problem in It is working fine and saving the values of Status
and Cancellation_Date
into the another database table. But the On change cancel_id value is not saving. How can I pass and save the onchange cancel_id
?
谢谢,非常感谢您的帮助.
Thanks and I really would appreciate your help.
推荐答案
您可以执行以下操作:
html :
<%= link_to_function "ommit", "ommit('#{@ommit.id}')" %>
js :
function ommit(id)
{
$.ajax({
url:"/your_controller_name/save_loc_values",
dataType: "json",
data: "ommit_id=" + id + "&cancel_id=" + $('#cancel_id').val(),
type: 'GET/POST'# your method type
});
}
控制器:
def save_loc_values
@area = Project::Area.find(params[:area_id])
@return = @area.save_loc_values(params[:cancel_id])
end
模型:
def save_loc_values(cancel_id)
update_record = Project::Loc.find_by_Project_Reference(project.Project_Reference)
update_record.update_attributes({
Status: 'D',
Cancellation_Date: DateTime.now().strftime("%Y-%m-%d %T.%L"),
CancellationReason_id: cancel_id
})
end
这篇关于如何在不提交表单的情况下将值传递给Rails模型方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文