0

I have an event form with some nested attribute models. The additional models are rendered after a client is selected from a select box. An observer watches and calls a controller action which renders a partial containing the fields_for nested models. The issue I'm having is that I can't pass the event 'form' block to the newly rendered partial - at least I can't figure out how...

The code below raises the error: "wrong number of arguments (0 for 1)". Any help or suggestions are appreciated. As mentioned below, I'm also willing to re-implement this using unobtrusive JavaScript if you can provide an example for this scenario.

Event Form:

<%- form_for @event do |form| %>

  <%= select_tag :id=>event_client_id %>
  <%= observe_field :event_client_id, url => {:action => 'client_questions'}, :with => "'client_id=' + encodeURIComponent(value)+'&event_id='+#{@event.id} %>

Event Controller

 def client_questions
   @event = Event.find(params[:event_id])
   @client = Client.find(params[:client_id])
   @client_questions = @client.questions.active
   respond_to do |format|
     format.js {
       render :update do |page|
         page[:client_questions].replace_html :partial => 'client_questions', :layout => false
       end
     }
   end
 end

_client_questions.html.erb partial

<%- form.fields_for :client, @client do |client| %>
  <%= client_text_field :name %>

  <%- client.fields_for :questions do |question| %>
    <%=question.text_field :content %>
3
  • One advice: use unobstrusive javascript en.wikipedia.org/wiki/Unobtrusive_JavaScript Commented Oct 7, 2010 at 12:47
  • I'm willing to make that change. Can you provide an example for this scenario? I'm obviously using prototype in this application so you can leverage that library. Commented Oct 7, 2010 at 13:08
  • Using unobtrusive js doesn't make any difference in this scenario. While it is considered a best practice it isn't relevant here. I'd like this app to be unobtrusive but since I inherited someone elses code I didn't have that choice. Commented Oct 8, 2010 at 13:52

1 Answer 1

3

So your main form is rendered in one action, and you're hoping to pass the form object from that view, to another view rendered in a second partial? That's not something that you can do, but you can modify your view code in the partial rendered the second time, so that it renders without needing the existing form object, something like:

<%- fields_for "event[client_attributes]", @client do |client| %>
  <%= client_text_field :name %>

  <%- client.fields_for :questions do |question| %>
    <%= question.text_field :content %>

I haven't run this code to test it, so compare the html it generates with the html that your existing partial would generate if it was rendered in the same action as the main form.

Sign up to request clarification or add additional context in comments.

3 Comments

Good idea! I'm going to try this out. I'll get back to you on my progress and see if it works.
It does work with a small change. It needs to read "event[client_attributes]", .... because it is a nested attribute model
Glad to hear Nate, I'll update the answer for others reference.

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.