Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd section to form by rendering a partial when link is clicked
    primarykey
    data
    text
    <p><strong>UPDATE 3:</strong></p> <p>For anyone who reads this, this is why it wasn't working as expected in update 2 below: <a href="https://stackoverflow.com/questions/12376653/passing-a-local-variable-to-a-partial-that-is-rendered-after-the-view-has-alread">Passing a local variable to a partial that is rendered after the view has already loaded</a> </p> <p>If anyone knows how to solve that issue, let me know please. </p> <p><strong>UPDATE 2:</strong></p> <p>I updated the javascript with the quotation marks and it partially works...in the sense that the javascript is now functional and it will cause a string of text to appear on the page when I click the link as long as I have the partial only contain a string of text. However, when the partial includes the form fields code, something goes wrong. </p> <p>If I just paste the following render code directly into the form in the new.html.erb view, it produces a new form section properly.</p> <pre><code> &lt;%= render "add_round", f: f %&gt; </code></pre> <p>However, when I try to include similar code in comps_helper.rb and then reference it from the link_to, it does not work: </p> <p>In comps_helper.rb:</p> <pre><code>def addRound(f) render "add_round", f: f end </code></pre> <p>In new.html.erb: </p> <pre><code> &lt;%= link_to "render it!", addRoundLink_path, remote: true %&gt; &lt;div id="some_id"&gt;&lt;/div&gt; </code></pre> <p>And I changed addRoundLink.js.erb to:</p> <pre><code>$("#some_id").html("&lt;%=j addRound(f) %&gt;"); #Is this the correct change to have made here? </code></pre> <p>Clicking the link_to link does nothing in that case. </p> <p>Any thoughts? </p> <p><strong>UPDATED CODE:</strong></p> <p>Thanks for the reply. I've made the following changes and it still does not appear to be working. The link appears at the bottom of the form but when clicked does not change anything. What am I missing?</p> <p>routes.rb: </p> <pre><code>resources :comps match '/new_competition', :to =&gt; "comps#new" get "/addRoundLink" =&gt; "comps#addRoundLink", :as =&gt; :addRoundLink </code></pre> <p>Note: I included the other 2 lines related to "comps" just in case those would cause an issue. </p> <p>comps_controller.rb: </p> <pre><code>def addRoundLink respond_to do |format| format.js end end </code></pre> <p>comps_helper.rb:</p> <pre><code>def addRound render "add_round" end </code></pre> <p>addRoundLink.js.erb:</p> <pre><code>$("#some_id").html(&lt;%=j addRound %&gt;); </code></pre> <p>comps/new.html.erb:</p> <pre><code>&lt;%= link_to "render it!", addRoundLink_path, remote: true %&gt; &lt;div id="some_id"&gt;&lt;/div&gt; </code></pre> <p>Thanks. </p> <p><strong>ORIGINAL QUESTION</strong></p> <p>First off, I'm new to rails. I've read and tried many solutions to similar questions but nothing has worked so far. </p> <p>I created a form with rails form_for and fields_for. The form creates a new competition (comp). The competition has many rounds. The top half of the form (the form_for section) accepts the details about the competition as inputs and the bottom half of the form accepts details about each round (the fields_for section). The form works perfectly in this basic format. </p> <p>I took all the code that is in the fields_for section and put it into a partial. My plan was to then create a "add new round" link to the bottom of the form that would simply display the partial above the link each time the link is pressed. This would add a new section to the form for a new round and allow the user to input as many rounds as they'd like. This is the part that I am struggling to make work.</p> <p>I added this code to my comps_helper: </p> <pre><code>def addNewRound render "add_round" end </code></pre> <p>This renders the file /views/comps/_add_round.html.erb. </p> <p>My question is: how do I get this to render in the form when a link is clicked. As far as I can get with the research I have done is: </p> <pre><code>&lt;%= link_to "Add new round", { }, :remote =&gt; true %&gt; </code></pre> <p>I don't exactly know what is supposed to go in the {} that will execute the addNewRound method. And I don't know what, if anything, I need to add to my comps_controller file. </p> <p>Thanks so much for the help.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload