Reputation: 4045
I'm trying to figure out how to pass data with ajax using a checkbox in Rails 3.2.11. I have the following line in my view
<%= check_box_tag(
"institution_ids_#{inst.name.gsub(" ", "")}",
inst.id,
false,
data: {
remote: true,
institution_id: inst.id}) %>
When I change the status of the checkbox, I can see that the controller is correctly getting called (specifically the index method of the controller, which is what I want as that is the view I'm in), however, I cannot seem to access the institution_id variable from the params hash on the controller. Can someone please explain how I use ajax to pass data from the view to the controller within a checkbox. I thought the remote: true function would correctly call the controller (which is does) with the additional parameters that I set (which it doesn't).
Upvotes: 2
Views: 5382
Reputation: 10137
Why don't you use jquery to make Ajax
call.
<%= check_box_tag( "institution_ids_#{inst.name.gsub(" ", "")}", inst.id, false %>
<script>
$('#checkbox_id').change(function(){
$.get('controller/action?inst_id='+$(this).val(), function(data,status){
if(status == 'success'){
alert(data)
}
})
})
</script>
In controller:
def action
inst_id = params[:inst_id]
#do something
render :text => "success"
end
Upvotes: 5