Add a section to form by partially highlighting the link

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) %>"); #Is this the correct change to have made here? 

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.

+3
ruby-on-rails
source share
1 answer

You need to create an action in the controller

application / controllers / some_controller.rb

 def hello respond_to do |format| format.js end end 

and determine the route for this action.

routes.rb

 get "/hello" => "some#hello", :as => :hello 

then create a link to this action as follows:

 <%= link_to "render it!", hello_path, remote: true %> <div id="some_id"></div> 

When you click this link, it will find its path to your action and will reply js (javascript), because we said that only js needs to be answered.

At the end, draw a partial place anywhere you want in your view (* in this example for some_id div *)

application / views / some / hello.js.erb

 $("#some_id").html("<%=j addNewRound %>"); 

A WARNING. Creating dynamic forms is a pain. You will have many problems (for example, setting different identifiers for new form elements, etc.). I highly recommend you use ryan bates nested_form gem

+2
source share

All Articles