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' %>
Means
  • 322
  • 2
  • 4
  • 16
  • 1
    Possible duplicate of [Rails form\_for renders partial form\_for to list values without submitting to DB, but now 2 submits.Help pls](http://stackoverflow.com/questions/42432861/rails-form-for-renders-partial-form-for-to-list-values-without-submitting-to-db) – cefigueiredo Feb 25 '17 at 05:55
  • I explained they are totally different issues. one for 2 form submits-pure rails issue and this is a JS+rails issue for simply rendering form as listings. You are marking my own answer to another question as 'duplicate' because I told you below.I'm not sure you have understood how both differ. – Means Feb 25 '17 at 06:29

1 Answers1

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 } %>")
cefigueiredo
  • 738
  • 3
  • 12
  • 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 #<#):. Can you tell me how to resolve local 'f'? – Means Feb 24 '17 at 18:10
  • 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` – cefigueiredo Feb 24 '17 at 18:55
  • Tip: To render a input independent of form_for... you should use the FormTagHelpers not FormHelpers http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html – cefigueiredo Feb 24 '17 at 19:00
  • 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. – Means Feb 24 '17 at 21:13
  • 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 – cefigueiredo Feb 24 '17 at 22:20
  • Actually, degree is a polymorphic form, passed in locals as degreeable: @application but I thought it simply confuses the question.The main form(@application) and polymorphic form (degree) are all working and entering into DB after so many tries. I posted 2-3 questions on SO but no one responded(maybe, polymorphic scared!).I did not think this simple display issue will interfere with polymorphism. Here's the full question and I only answered it-http://stackoverflow.com/questions/42432861/rails-form-for-renders-partial-form-for-to-list-values-without-submitting-to-db/42443732#42443732. – Means Feb 25 '17 at 05:40
  • if you are wondering there's more code in partial as it starts with fields_for, surprisingly, there's none and that's all to it. See the answer in question above where I've shown the source where I found it. – Means Feb 25 '17 at 05:45
  • Ok... good that you figured out how to solve... next time try make questions adding the all relevant code... I was lost trying figure out about what you are asking about... the entire form and the partial in the other question made total difference! – cefigueiredo Feb 25 '17 at 05:51
  • here's the log when submit button is pressed - {"utf8"=>"✓" "authenticity_token"=>"RX+mQ==","application"=>{"title"=>"", "company_id"=>"7","closing_date"=>"","degree"=>{"level"=>"High School", "description"=>"cvcvbcvbc"}},"commit"=>"Create Application"}.As you see, degree is a nested block in application but there's 1 record. Like that I want user to press add and list more degrees...all then saved while application creation. – Means Feb 25 '17 at 05:54
  • Sorry about the confusion :-) 'f' is locally referred in both main & partial. I feel it's 'f' sent as form. how to pass it again as form to partial – Means Feb 25 '17 at 06:08