Rails form_for added empty model - ruby-on-rails

I have started the "Get Started Guide" from the ruby on rails website. Everything works fine, but when I change the order of showing all comments and than display the comments-form in the other way round, than the form_forfunction adds a empty comments model to #post.comments and so, I display one empty comment in the loop.
Here is the view:
<h1><%= #post.name %></h1>
<p><%= #post.text %></p>
<h2>Add comment</h2>
<%= form_for([#post, #post.comments.build]) do |f| %>
<p>
<%= f.label :name %><br>
<%= f.text_field :name %>
</p>
<p>
<%= f.label :email %><br>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<h2>Comments</h2>
<%= render #post.comments %>
The loop display two comments. One, that exists in the db and one, that has just empty attributes. If I delete the form, than all is shown up correct.

You can select only your persisted comments:
<%= render #post.comments.select(&:persisted?) %>

When you do post.comments.build against some post, it will be added to the post.comments collection and will be displayed along with other comments.
You can always use persisted to check if the object is present in database meaning id is assigned to it.
#post.comments.select(&:persisted?)
Note: .present? check donot work here so you have to use .persisted.?
present check only assosiated to the parent.

Related

Placing Rails Form_For Fields in Different Locations

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 %>

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.

Form not showing up - Ruby on Rails

I'm currently following a Ruby on Rails tutorial book and I've noticed that the newer version of Rails is quite a bit different. A couple of given commands were different than described in the book and I've had to look up a few ways on how to fix these things. Right now though, I don't know what's going wrong. I have created a database table products and I'm simply trying to use a form to display some input components etc to create a new product. The book told me to do this:
<h1>New product</h1>
<% form_for(#product) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<p>
<%= f.label :description %><br />
<%= f.text_area :description, :rows => 6 %>
</p>
<p>
<%= f.label :image_url %><br />
<%= f.text_field :image_url %>
</p>
<p>
<%= f.label :price %><br />
<%= f.text_field :price %>
</p>
<p>
<%= f.submit "Create" %>
</p>
<% end %>
<%= link_to 'Back', products_path %>
However, the view only shows me the New product header and the link to go back. I've already installed a different gem because the f.error_messages apparently wasn't used anymore either. The problem is, the entire form_for part does not show up anything. Can anybody tell me how I am supposed to change this code to get it to show up on the view for creating a new product?
Thanks!
This is what it shows:
You are missing = here <% form_for(#product) do |f| %>. It should be
<%= form_for(#product) do |f| %>

Active Admin, one page - two diffrent forms

I have following two resources:
Position
JobTitle
And a position belongs_to job title.
In page Position, I create new position, with job_title select and some other selects. I need also create new job_title in this page, instead of choose select variants. So, in fact, or I choose job title in existing base, or create new, and save it for this position.
How can I make this? I looking for answer, and reading docs, but nothing!
You need to use fields_for in your view to send params for two models at a time.
Here's how I would try to solve to problem:
<%= form_for #job_title do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :description %><br />
<%= f.text_field :description %>
</p>
<%= f.fields_for :position do |builder| %>
<p>
<%= builder.label :name %> <br />
<%= builder.text_field :name %>
</p>
<% end %>
<p><%= f.submit "Submit" %></p>
<% end %>
And then, in your controller, you need to create the association. For more information, you can check out the railscast for nested forms.

In Rails, how do I generate form labels without symbols that still create correct "for" attributes?

In Rails, how do I generate form labels without symbols that still create correct "for" attributes?
If I take this form:
<% form_for(#thing) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
And alter it to improve the clarity of what's expected in the field:
<% form_for(#thing) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label "What would you like to call your thing?" %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
The for attribute on the label tag would read "thing_What would you like to call your thing?", which obviously destroys its relationship with the intended partner field.
So how do I alter the label text while preserving that relationship?
<%= f.label :name, "What would you like to call your thing?" %>
See label’s documentation (and on APIdock).

Resources