Rails的check_box_tag如何ajaxily检查时,传递价值 [英] Rails check_box_tag how to pass value when checked ajaxily
问题描述
在我的索引页为我的任务模式,我想告诉每一个在我的任务数据库表中对应于布尔域完全排了一个复选框。
目前我的code进入方法完成,但它不包含用户只是做了该复选框(的价值,即如果他们只是检查中,不通过真实的我完成法)。
如何传递用户刚刚完成的价值 - 无论是检查或取消检查
/views/tasks/index.html.erb
<%@ tasks.each_with_index办|任务,我| %>
&其中; TR>
< TD><%= check_box_tag完整,task.complete,task.complete,:数据=> {:远程=>如此,:URL => url_for(:动作=>完整的,:ID => task.id,:完整=> task.complete):方法=> :把}:类=> 投入大的%>< / TD>
< / TR>
<%结束%GT;
/控制器/ tasks_controller#完整
#PUT /完整/ 1
高清完整
@task = Task.find(PARAMS [:ID])
P内完成
P完整=#{PARAMS [:完整]}
@ task.complete =
如果@ task.update_attributes(PARAMS [:任务])
P里面的更新
渲染:文本=> 成功
其他
P内部错误
结束
结束
如果你正在使用jQuery,你可以写一个click事件。
$('。输入大)。点击(函数(){
VAR检查;
如果($(本)。是(:选中)){
检查=真;
} 其他 {
检查=虚假的;
}
$阿贾克斯({
键入:POST,
网址:/任务/完成,
数据:{ID:$(本)的.data(后ID),检查:检查}
});
});
On my index page for my Task model, I want to show a checkbox for every row that corresponds to the boolean field "complete" in my Task database table.
Currently my code gets into the method "Complete", but it does not contain the value of the checkbox that the user just did (i.e. if they just checked the box, it does not pass true to my "Complete" method).
How can i pass the value that the user just performed - either checked or un checked?
/views/tasks/index.html.erb
<% @tasks.each_with_index do |task, i| %>
<tr>
<td><%= check_box_tag 'Complete', task.complete, task.complete, :data => {:remote => true, :url => url_for( :action => 'complete', :id => task.id, :complete => task.complete ), :method => :put}, :class => 'input-large' %></td>
</tr>
<% end %>
/controllers/tasks_controller#complete
# PUT /complete/1
def complete
@task = Task.find(params[:id])
p "inside complete"
p "complete = #{params[:complete]}"
@task.complete =
if @task.update_attributes(params[:task])
p "inside update"
render :text => "success"
else
p "inside error"
end
end
If you are using jQuery, you can write a click event.
$('.input-large').click(function() {
var checked;
if ($(this).is(':checked')) {
checked = true;
} else {
checked = false;
}
$.ajax({
type: "POST",
url: "/tasks/complete",
data: { id: $(this).data('post-id'), checked: checked }
});
});
这篇关于Rails的check_box_tag如何ajaxily检查时,传递价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!