How to hide database records from view? - ruby-on-rails

I'm beginner in Ruby On Rails and I'm reading "Getting Started with Rails" now.
I created models and controllers for Article and Comments, everything ok, I'm able to add comments and they appear on Article view, but besides I see records from database in this format:
[# Comment id: 1, commenter ... updated_at: "2016-05-20 09:26:25"]
Why they appear and how to hide it? Code of Article's view show.html.erb:
<p>
<strong>Title:</strong>
<%= #article.title %>
</p>
<p>
<strong>Text:</strong>
<%= #article.text %>
</p>
<h2>Comments</h2>
<%= #article.comments.each do |comment| %>
<p>
<strong>Commenter:</strong>>
<%= comment.commenter %>
</p>
<p>
<strong>Comment:</strong>>
<%= comment.body %>
</p>
<% end %>
<h3>Add a comment</h3>
<%= form_for([#article, #article.comments.build]) do |f| %>
<p>
<%= f.label :commenter %>
<%= f.text_field :commenter %>
</p>
<p>
<%= f.label :body %>
<%= f.text_area :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<%= link_to 'Edit', edit_article_path(#article) %>
<%= link_to 'Back', articles_path %>

Instead of:
<%= #article.comments.each do |comment| %>
It should be:
<% #article.comments.each do |comment| %>
<%= %> used to render something. And you are iterating an object so you need to use <% %>.

Change this:
<% #article.comments.each do |comment| %>
What <%= does is displaying whatever data he's dealing with. If you do <% #article.comments.each do |comment| %> it will deal with the data as well but he won't display anything. And then you can display whatever you want with:
<%= comment.commenter %>
<%= comment.body %>

Related

Why is post title not showing up?

So here it is:
For some reason this is not showing the title for my post.
However when I press show option and it goes to post's page you can see al the detail.
_form.html.erb
<%= form_with(model: post, local: true) do |form| %>
<% if post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% post.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :date %>
<%= form.datetime_select :date, id: :post_date %>
</div>
<div class="field">
<%= form.label :name %>
<%= form.text_area :name, id: :post_name %>
</div>
<div class="field">
<%= form.label :user_id %>
<%= form.number_field :user_id, id: :post_user_id, value: current_user.id %>
</div>
<div class="field">
<%= form.label :description %>
<%= form.text_area :description, id: :post_description %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
#show.html.erb
<p id="notice"><%= notice %></p>
<p>
<strong>Date:</strong>
<%= #post.date %>
</p>
<p>
<strong>Name:</strong>
<%= #post.name %>
</p>
<p>
<strong>User:</strong>
<%= #post.user_id %>
</p>
<p>
<strong>Description:</strong>
<%= #post.description %>
</p>
<% if current_user == #post.user %>
<%= link_to 'Edit', edit_post_path(#post) %> |
<%end%>
<%= link_to 'Back', posts_path %>
<h1>Editing Post</h1>
<%= render 'form', post: #post %>
<%= link_to 'Show', #post %> |
<%= link_to 'Back', posts_path %>
edit.html.erb
<%= render 'form', post: #post %>
<%= link_to 'Show', #post %> |
<%= link_to 'Back', posts_path %>
I've tried changing it <%= form.text_area :name, id: :post_name %>
to string_area :name and also string_field but none of it is working.
In fact when I change it to the last 2, I get a method error if I go on the edit page. I'm still fairly new to rails and to ruby so I would greatly appreciate some help. I did try to google however and was unsuccessful, I guess I didn't know what to search for or maybe I worded it wrong.

If condition in html.erb rails

How can I place an if condition in the HTML, in pseudocode, I'd like to achieve the following:
if #time.status is pending
place edit and delete below the message
else
not just display
end
This is my current code:
<% #value.each do |s| %>
<p><strong>Message:</strong>
<%= s.message %>
</p>
<p><strong>Date</strong>
<%= s.date %>
</p>
<p><strong>Status:</strong>
<%= s.status %>
</p>
<p>
<%= link_to 'Edit', value(), %>
<%= link_to 'Delete', value(), %>
</p>
If you have access to #time and status is an attribute of it, with a value that can be "pending" (thing you didn't add in the question), then you can do:
<% if #time.status == 'pending' %>
<!-- place edit and delete -->
<% else %>
<% #value.each do |s| %>
<p>
<strong>Message:</strong>
<%= s.message %>
</p>
<p>
<strong>Date</strong>
<%= s.date %>
</p>
<p>
<strong>Status:</strong>
<%= s.status %>
</p>
<p>
<%= link_to 'Edit', value(), %>
<%= link_to 'Delete', value(), %>
</p>
<% end %>
<% end %>
It is same as your iteration
<% if #time.status=="PENDING" %>
#your html code for edit in delete
<%end%>

How to post array of objects in rails?

I have the following code in my
new.html.erb
<%= form_tag puppies_path do %>
<% #kennel.each do |puppy| %>
<%= fields_for 'puppies[]', puppy do |p| %>
<div class="field">
<%= p.label :name %><br>
<%= p.text_field :name %>
</div>
<div class="field">
<%= p.label :breed %><br>
<%= p.text_field :breed %>
</div>
<% end %>
<% end %>
<div class="actions">
<%= submit_tag %>
</div>
<% end %>
And puppies[] array variable, which is supposed to post array of objects to controller is posting only single object. Please help to post an array to controller. Thanks in advance!
The usual setup for fields_for is something like this.
<% #kennel.each do |kennel| %>
<%= fields_for :puppies, #kennel.puppies do |p| %>
Yes, I have just found an answer...
In new.html.erb file
<%= form_tag puppies_path do %>
<% 2.times do %>
<%= render 'puppies_group_form' %><br>
<% end %>
<%= submit_tag "Submit" %>
<% end %>
In _puppies_group_form
Name <%= text_field_tag "puppies[][name]" %>
Breed <%= text_field_tag "puppies[][breed]" %>

Rails view error (on nitrous io IDE) syntax error, unexpected keyword_ensure, expecting end-of-input

i'm having problems with my code ,i get this message above.here is my code .i'm new to ruby
app/views/articles/edit.html.erb
<h>Edit existing article</h>
<%=#article.errors %>
<h2>The following errors prevented the article from getting created</h 2>
<ul>
<%#article.errors.full_messages.each do |ms g| %>
<li><% ms g %></l i>
<% end %>
</ul>
<% end %>
<%= form_for #article do |f| %>
<p>
<%= f.label :title %><b r/>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :description %><br/>
<%= f.text_area :description %>
</p>
<p>
<%=f.submit %>
<% end %>
You seem to have a cut and paste, or perhaps an editor issue - there are several lines with breaks between words.
And you have an extra <% end %> tag that doesn't seem to line up to anything
Try this instead
<h>Edit existing article</h>
<%=#article.errors %>
<h2>The following errors prevented the article from getting created</h2>
<ul>
<%#article.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<%= form_for #article do |f| %>
<p>
<%= f.label :title %><br/>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :description %><br/>
<%= f.text_area :description %>
</p>
<p>
<%=f.submit %>
<% end %>
You have a bunch of spaces where you probably didn't intend them (including the text in your post above).
I think you meant for ms g to be msg on this section:
<%#article.errors.full_messages.each do |msg| %> # `ms g` => `msg`
<li><% msg %></li> # `ms g` => `msg`
Also remove the space in </l i> and <b r/>

form_for details not displaying in view :Ruby on Rails

Hi I am using simple form_for to create new object but on the ads/new.html.erb on h1 tag is displayed but form's information is missing
Here is my new.html.erb file contents
<h1>New Ad</h1>
<% form_for(#ad ,:url=>{:action=>'create',method: :post}) do |f| %>
<p>
<b>Name:</b> <%= f.text_field:name %>
</p>
<p>
<b>Description:</b><%= f.text_area:description %>
</p>
<p>
<b>Price:</b><%= f.text_field:price %>
</p>
<p>
<b>Seller Id:</b><%= f.text_field:seller_id %>
</p>
<p>
<b>Email Address:</b><%= f.text_field:email %>
</p>
<p><%= f.submit "Create Ad" %>
</p>
<% end %>
This line
<% form_for(#ad ,:url=>{:action=>'create',method: :post}) do |f| %>
should be like this
<%= form_for(#ad ,:url=>{:action=>'create',method: :post}) do |f| %>
You are missing = sign
Reference,see the API
Additional note
I guess you can refactor it to
<%= form_for(#ad) do |f| %>

Resources