Placing Rails Form_For Fields in Different Locations - ruby-on-rails

I have a model to be fill with a form, consisting of a title and a body of text. Optionally, I want the ability to submit a link as well as a part of this model. If this is filled out, it is submitted, otherwise ignored. The form would have title and body fields at top of page, for instance.
<%= form_for(#micropost, remote: true) do |f| %>
<div class="field">
<%= f.text_field :title %>
<%= f.text_area :content %>
</div>
<%= f.submit "Post" %>
<% end %>
Now, I want to add the field for the link, but in a different location on the page (so as to indicate that it is optional).
<%= f.text_field :link %>
I tried doing this all in one partial,
<%= form_for(#micropost, remote: true) do |f| %>
<div class="field">
<%= f.text_field :title %>
<%= f.text_area :content %>
<! insert other content here >
<%= f.text_field :link %>
</div>
<%= f.submit "Post" %>
<% end %>
But this would lead to very messy nesting of partials and I'm not sure how to get this to work correctly. Alternatively, I was wondering if it was possible to have one form_for at the top of the page, and another form_for at the bottom of the page that are somehow "synced", so that by pressing the submit button at the top, the value entered in the bottom form_for is collected and submitted as well.
<%= form_for(#micropost, remote: true) do |f| %>
<div class="field">
<%= f.text_field :title %>
<%= f.text_area :content %>
</div>
<%= f.submit "Post" %>
<% end %>
<%= form_for(#micropost, remote: true) do |f| %>
<! somehow sync this with the other form >
<%= f.text_field :link %>
<% end %>

One option is to have an auxiliary text field for link attribute. Copy it's value to hidden variable mapping the link attribute
Assuming you are going to use jQuery,
<%= form_for(#micropost, remote: true) do |f| %>
<%= f.hidden_field :link %>
<div class="field">
<%= f.text_field :title %>
<%= f.text_area :content %>
</div>
<%= f.submit "Post" %>
<% end %>
Somwhere on the same page
<%= text_field_tag 'micropost[link]', '', id: 'aux_link' %>
<script>
$(function(){
$('form').on('submit', function(){
$('#micropost_link').val($('aux_link'));
});
});
<script>
This is just an approach. Adjust code as per your form element ids.
One issue with this approach is it will be difficult to validate if link attribute is compulsory.

Just make the form_for be the root of the view
You can just use the form_for like a container div.
Then put the <input> and <button> as you want like making your normal webpage.
Then, because you want to make some inputs optional, you can do that in rails controller.
def create_or_update
params[:micropost].delete(:link) if params[:micropost][:link].blank?
# continue the task
end
This trick is useful for all optional fields like password

I found here that the canonical way to do this is to use the "form" attribute. Give the form an id, from which the field can refer back to the form, even if the field is placed outside of the form. For instance,
<%= form_for(#micropost, remote: true, html: { id: "micropost_form" }) do |f| %>
<div class="field">
<%= f.text_field :title %>
<%= f.text_area :content %>
</div>
<%= f.submit "Post" %>
<% end %>
We can then use fields_for to place the desired field elsewhere, linking back to the original form.
<%= fields_for #micropost do |f| %>
<%= f.text_field :link, form:"micropost_form" %>
<% end %>

Related

creating a dropdown list in a form_for helper referring to another model

im new to ruby on rails, im trying to create a dropdown list in a form_for helper referring to another model (users). basically, im trying to select from list of users so that their email will be copied to the receiver field.
here's what i have so far, i can only manually type in the email address:
<%= form_for :femail, url: {action: "create"} do |form| %>
<p>
<%= form.label :from %><br>
<%= form.text_field :from %>
</p>
<p>
<%= form.label :to %><br>
<%= form.text_area :to %>
</p>
<p>
<%= form.label :subject %><br>
<%= form.text_field :subject %>
</p>
<p>
<%= form.label :body %><br>
<%= form.text_area :body %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
thanks in advance.
You'll need to have your set of users coming from somewhere (maybe your controller?), I'm going to call it #users. Then you can do something like:
<p>
<%= form.label :to %><br>
<%= form.select(:to, options_for_select(#user.map { |u| [u.name, u.email]}) ) %>
</p>
That will show you a set of options that display the user.name, but will actually submit the value user.email. The select tag has some pretty useful options (e.g. setting default values, not accepting blank entries, etc).
More info: this section of the Rails guides and the FormOptionsHelper docs.

Nested route values not populating edit Rails

I have a belongs_to relationship between a map and a row. A map has_many rows.
For some reason, when I try and edit a row, even when it is populated in my database, it is not showing the values when I try to edit this row. Why would this be? Below is the _form.html.erb and the edit.html.erb file.
Edit.html.erb
<%= render 'form', row: #row %>
_form.html.erb
<%= form_for [#map, #map.rows.build], method: :post, url: map_rows_path do |form| %>
<div class="field">
<%= form.label :timeframe %>
<%= form.text_field :timeframe, id: :timeframe %>
</div>
<div class="field">
<%= form.label :standards %>
<%= form.text_field :standards %>
</div>
<div class="field">
<%= form.label :content %>
<%= form.text_field :content %>
</div>
<div class="field">
<%= form.label :skills %>
<%= form.text_field :skills %>
</div>
<div class="field">
<%= form.label :resources %>
<%= form.text_field :resources %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
If I try Row.last in the rails console it confirms that this row is populated with data. I am assuming this has something to do with the way the form is setup however I am not familiar with these relationships. How can this be fixed?
it seems you are passing row to form where <%= render 'form', row: #row %> but in Actual edit form you are using a variable, #map and building a blank row object by saying #map.rows.build, hence the row object will be a new object with out any values.
Based on your code change it to following and see. ( I am assuming you are setting up #map object in your edit action in controller)
<%= form_for [#map, row], method: :post, url: map_rows_path do |form| %>

Rails 4 ajax load partial into view

there is a way to put an input dynamically from button?
Example:
this is the form:
<%= form_for(#order) do |f| %>
.
...
<div class="field">
<%= f.label :productlist %><br>
<%= f.select :productlist, #products, :prompt => 'Select one!' %>
<%= link_to 'Add More', '#', id: 'products-link' %>
</div>
<div id="newproduct">
<p>load here the new input on Add more link</p>
</div>
<div class="field">
<%= f.label :status %><br>
<%= f.text_field :status %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
so i thinked up to ajax and a way to load a partial input into the form, but im not understanding the correct way to do this.
Thank you
In your javascript file add:
$("a#products-link").click(function(){
$.get('/partials/products_form', function(data){
$("div#newproduct").append(data);
}, 'html');
});
Essentially what is happening is that you are going to send a get request via ajax to a form in /partials/products_form or wherever you choose to keep your form partial. And then this html will be appended to whatever div/identifier you choose in this case div#newproduct.

how do I create a button_to make a new entry_field within a form.

I want to make a button that will allow me to create a new text_field for :phone_number in addition to the one I already have. For example if I want to submit two phone number instead of one in separate fields how would that be accomplished? In the code below I have begun to make a button_to but am not sure what actions to take in order to make a new field of entry. thanks.
<div>
<%= form_for #reminder do |f|%>
<%= f.label "Your Reminder"%>
<%= f.text_area :text %>
<!-- button here to make new text_field to input in an extra phone number -->
<%=button_to "add another number", %>
<%= f.label "Phone Number" %>
<%= f.text_field :phone_number %>
<%= f.label "When to be sent" %>
<%= f.text_field :time%>
<%= f.label "Picture URL" %>
<%= f.text_field :picture %>
<%= f.label "Favorite?" %>
<%= f.check_box :favorite %>
<%= f.submit %>
<% end %>
</div>
You can accomplish this using JavaScript. Get the id of the button and then add the new input to the page when clicked.

Rails: Using AJAX to delete a form field

I have a nested form where an workout has many exercises, and I'm trying to allow the user to delete and add exercises to the form without having to submit the form.
<%= form_for(#workout) do |f| %>
<%= f.label :name %><br />
<%= f.text_field :name %><br />
<%= f.label :description %><br>
<%= f.text_area :description %></br>
<%= f.fields_for :workout_exercises do |workout_exercise| %>
<fieldset>
<%= workout_exercise.text_field :name %>
<%= workout_exercise.hidden_field :_destroy %>
<%= link_to "Remove Exercise", '#', class: "remove_fields" %>
##This is what I want to do ##
<%= link_to 'Remove Exercise', set, remote: true, class: "remove_fields delete", method: :delete %>
</fieldset>
<% end %>
Right now I can make the fields fade out, but to save the change to the DB I need to click the "update workout" button at the bottom of the form. I want the change to take effect as soon as the "Remove Exercise" button is pressed, otherwise, users will think an element has been deleted when it's actually just been hidden.
Any ideas?
Thanks!
I would strongly recommend the Cocoon gem for this. You can add events before and after a nested fieldset is inserted as needed.

Resources