UPDATE 3:
For everyone who reads this, so it did not work properly in Update 2 below: Passing the local variable partial, which is displayed after the view is already loaded
If anyone knows how to solve this problem, please let me know.
UPDATE 2:
I updated javascript with quotes, and it partially works ... in the sense that javascript is now functioning, and it will make a line of text appear on the page when I click the link, while I have a partial contains only a line of text. However, when partial includes code for form fields, something goes wrong.
If I simply paste the following visualization code directly into the form in the new.html.erb view, it creates the new section of the form correctly.
<%= render "add_round", f: f %>
However, when I try to include similar code in comps_helper.rb and then link to it with link_to, it does not work:
In comps_helper.rb:
def addRound(f) render "add_round", f: f end
In new.html.erb:
<%= link_to "render it!", addRoundLink_path, remote: true %> <div id="some_id"></div>
And I changed addRoundLink.js.erb to:
$("#some_id").html("<%=j addRound(f) %>");
When the link_to link is clicked, nothing happens.
Any thoughts?
UPDATED CODE:
Thanks for the answer. I made the following changes and does not seem to work. The link is displayed at the bottom of the form, but when clicked does not change anything. What am I missing?
routes.rb:
resources :comps match '/new_competition', :to => "comps#new" get "/addRoundLink" => "comps#addRoundLink", :as => :addRoundLink
Note. I included two other lines related to "comps", in case this causes a problem.
comps_controller.rb:
def addRoundLink respond_to do |format| format.js end end
comps_helper.rb:
def addRound render "add_round" end
addRoundLink.js.erb:
$("#some_id").html(<%=j addRound %>);
privileges / new.html.erb:
<%= link_to "render it!", addRoundLink_path, remote: true %> <div id="some_id"></div>
Thanks.
ORIGINAL QUESTION
Firstly, I am new to rails. I read and tried many solutions on similar issues, but so far nothing has worked.
I created a form with rails form_for and fields_for. The form creates a new competition (comp). There are many rounds in the competition. The upper half of the form (section form_for) accepts information about the competition as input, and the lower half of the form accepts information about each round (confession_fields). The form works great in this basic format.
I took all the code that is in the fields_for section and put it in partial. My plan was then to create a “add a new round” link at the bottom of the form, which would simply display a partial link above the link each time the link is clicked. This will add a new section to the form for the new round and allow the user to enter as many rounds as he wants. This is the part that I try my best to work with.
I added this code to my comps_helper:
def addNewRound render "add_round" end
This displays the file / views / comps / _add_round.html.erb.
My question is: how do I get this to render in the form when I click a link. As far as I can get the research that I have done is:
<%= link_to "Add new round", { }, :remote => true %>
I don’t know exactly what should be in {} which the addNewRound method will execute. And I don't know what, if you like, I need to add comps_controller to the file.
Thank you for help.