0

I wish to reload a partial-form with a button(Add). I'm new and don't know how to simply display fields in partial like listings one under the other as many times Add button is clicked. I can't find a relevant example. all AJAX examples mention js.erb when object is saved in DB.

<div class="panel-body">
 <%= render partial: "degrees/form", :locals => { :f => f }  %> 
 <%= f.link_to (fa_icon 'plus').to_s + " Add Another Qualification ", render(:partial => 'degrees/form', :locals => { :f => f }), class: "btn btn-primary" %>
</div>

Here, @application is the main form trying to display degree fields. Partial is simply two text-fields- one for selecting educational degree and second its detail.Here's partial

 <%= f.fields_for [Degree.new], :url => { :action => "index" } do |ff| %>
  <div class = "form-group" }>
    <%= ff.select :level, options_for_select(Job::EDUCATION, params[:level]), include_blank: "Select Degree", class: 'span-2' %>
    <%= ff.text_field :description, :class => 'span5' %>
  </div>
   <% ff.submit "Add Another Degree", class: 'btn btn-primary' %>
2

1 Answer 1

0

You don't necessary need to save things to pass away to .js.erb... you can just instantiate...

But if your example is precise, you are missing the remote: true flag for the link... And the partial is not defined on the link... you need to make a view responding to the ajax...

Form example

<div class="panel-body">
  <%= link_to new_thing_path, remote: true do %>
    <i class="fa fa-plus">
    Add another qualification
  <% end %>
  <div class="new-things-container">
  </div>
</div>

Controller answering to the ajax request

class ThingsController < ApplicationController
  def new
    @thing = Thing.new

    respond_with @thing
  end
end

View for the ajax request rendering a partial inside a specified div

//views/things/new.js.erb
$('.panel-body .new-things-controller').append("<%= render partial: 'degrees/form', locals: { thing: @thing } %>")
Sign up to request clarification or add additional context in comments.

10 Comments

Ok so there's an error while displaying partial fields as the partial actually is fields_for taking locals as f. The error it throws on console is:ActionView::Template::Error (undefined local variable or method `f' for #<#<Class:0x0....>):. Can you tell me how to resolve local 'f'?
So... it's the wrong question... What is f? Is it a form_for Helper? You cant pass it away... it is not an @object that exists out of it's Helper block... if you intended that it was an attribute of the @object you make a form with... you need pass the @object.attribute, not the f.input :attribute
Tip: To render a input independent of form_for... you should use the FormTagHelpers not FormHelpers api.rubyonrails.org/classes/ActionView/Helpers/…
Sorry, was busy into something and about to sleep.I'll be look again at it tomorrow but I didnot understand how to pass @object.attribute. Actually, I hope you realize that actually @object(@application) is only created but has yet got no id so when I call partial it expects to find that object. I've only seen syntax to pass locals{ . :f=>f } but not sure of another syntax of attribute to render form. and yes there's a little typo in your code in js.erb-> 'new-things-controller' must be '.new-things-container'.Many thx for your help. Please leave your comments and I'll get back to u.
Ok... when possible, edit your question to show the entire form... or the form declaration AND the relevant parts if too big... I would like to see the template of the mentioned partial too... I'm not sure if I understood your question... or it does not make sense if I did
|

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.