0

I am trying to update a list of radio options depending on the person that a user selects in a form.

_fields.html.erb:

<%= f.label :person %>
<%= f.select(:person_id, current_user.person_names) %>

<%= f.label :invoice_type %>
<%= radio_buttons_collection(f.object.invoice_types, f) %>

application_helper.rb:

def radio_buttons_collection(types, f) # works, but not with Ajax!
  types_html = types.map do |type|
    f.radio_button(:invoice_type, type)
  end
  safe_join(types_html)
end

projects_controller.rb:

def get_invoice_types
  person = Person.find(params[:person_id])  
  @types = person.address_types
end

get_invoice_types.js.erb:

$('#project_invoice_type').html("<%= escape_javascript(radio_buttons_collection(@types, f)) %>");

application.js:

$("#project_person_id").change(function() {
  $.ajax({
    url: '/projects/get_invoice_types',
    data: 'person_id=' + this.value,
    dataType: 'script'
  })
});

Everything works except that the radio options won't get updated when the person gets changed in the form.

Can anybody tell me how to do this?

Thanks for any help.

6
  • looks like form object in ajax is nil. Can you please check that form object i.e. f in get_invoice_types.js.erb is nil or not ? Commented May 20, 2013 at 14:29
  • Thanks for your help. Silly n00b question, but how exactly can I check that f is not nil? Commented May 20, 2013 at 15:05
  • you can try to print in application_hlper method. which should be call. Commented May 20, 2013 at 15:09
  • The problem is that the radio_buttons_collection(types, f) method doesn't get updated at all through Ajax. So I can't really test the output here. Commented May 20, 2013 at 15:25
  • Can you please paste form code ? Commented May 20, 2013 at 15:59

2 Answers 2

1

I will suggest not to pass form object to helper method

def radio_buttons_collection(types, invoice_type)
  types_html = types.map do |type|
    radio_button_tag("project[invoice_type]", type, type == invoice_type)
  end
  safe_join(types_html)
end

pass invoice_type to helper method along with types.

in controller define

@invoice_type = person.invoice_type

get_invoice_types.js.erb:

$('#project_invoice_type').html("<%= escape_javascript(radio_buttons_collection(@types, @invoice_type)) %>");

_fields.html.erb:

<%= f.label :invoice_type %>
<div id = 'project_invoice_type'>
  <%= radio_buttons_collection(f.object.invoice_types, f.object.invoice_type) %>
</div>
Sign up to request clarification or add additional context in comments.

8 Comments

OK, tried that, even with project[invoice_type] instead of form[invoice_type]. But it seems there's no interaction with the database when doing it this way. Nothing gets saved.
check params in server logs . is invoice_type present in params ?
add a div container to radio buttons which will be replaced by ajax request , as i did in new edit
Hello again, turns out my get_invoice_types.erb.js was the cause of all the misery. I had lines 1 and 2 commented out temporarily using double slashes (//) which commented out the code for TextMate (my code editor) but seemingly not for Rails. Thanks for your help again, though. I'll mark your answer as correct since I would have been lost without you on this one. Cheers!
Good to hear that you Finally made it :) Happy Coding
|
0

You need to trigger the change. Try this in your get_invoice_types.js.erb

$('#project_invoice_type').html("<%= escape_javascript(radio_buttons_collection(@types, f)) %>");
$('#project_invoice_type').trigger('change');

2 Comments

Are you seeing any errors in your rails server logs? Any javascript errors popping up when inspecting?
Yes! I am getting an Internal Server Error there in app/views/projects/get_invoice_types.js.erb:1. The raw HTML looks a bit ugly: undefined local variable or method f&#x27; for #&lt;#&lt;Class:0x007f9739358b80&gt;:0x007f973bdf0118&gt;. I guess it's trying to say that the local variable f is not there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.