If condition in html.erb rails - ruby-on-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%>

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.

How to hide database records from view?

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

Rails Same page with url paramaters only change

Actually this is my view am getting values from mysql database and i showed in browser based on the params page=1 page =2 page =3 and i want to change the params[:page] in form next button click.
Based on pages=1,2,3 questions and answers will vary thats why i need url to be change while button click in same page.! like below URL http://localhost:3000/responses/new?page=1 Thanks in advance.
<%= form_for (#response) do |f| %>
<% #questions.each do |pgquestion| %>
<% if pgquestion.group_id == 0 %>
<%= label :pgquest,pgquestion.description %><br><br>
<% else %>
<%= label :pgquest,pgquestion.description %>
<% (1..pgquestion.question_value.to_i).each do |i| %>
<%= radio_button_tag "ans_value[#{ pgquestion.quest_id }]", i %>
<% end %>
<br><br>
<% if pgquestion.question_type == "textarea" %>
<%= text_area "", "ans_value[#{ pgquestion.quest_id }]" ,size: '80x5' %><br><br>
<% else if pgquestion.question_type == "text" %>
<%= text_field "", "ans_value[#{ pgquestion.quest_id }]" %> <br><br>
<% end %>
<% end %>
<% end %>
<% end %>
<div style="text-align: center">
<%= f.submit 'Prev' ,:class => 'btn btn-primary',:name => "previous_button" %>
<%= f.submit 'Next', :class => 'btn btn-primary' %> <br><br>
<p><%= f.submit "Continue" %></p>
<p><%= f.submit "Back", :name => "previous_button" %></p>
</div>
<% end %>
As far as i understand your problem use the will_paginate gem https://github.com/mislav/will_paginate

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

undefined method `each' Problem/Question?

<% #company.contacts.each do |contactall| %>
<% contactall.each do |contact| %>
<%= contact.name %>
<%= contact.position %>
<%= contact.email%>
<%= contact.telephone%>
<%= contact.source%>
<%= contact.company_id%>
<% end %>
<% end %>
What's wrong with it?
I want to display each contact separately if i do take out <% contactall.each do |contact| %>
and do contact.name i get a bunch of the contacts name's that are associated with the company. help?
I want them all separate.]
The fix:
<% #company.contacts.each do |contact| %>
<b>Name:</b>
<%= contact.name %>
<b>Position:</b>
<%= contact.position %>
<b>Email:</b>
<%= contact.email%>
<b>Telephone:</b>
<%= contact.telephone%>
<b>Source:</b>
<%= contact.source%><br />
<% end %>
What do you mean separate? This?
<% #company.contacts.each do |contact| %>
<div>
<%= contact.name %>
<%= contact.position %>
<%= contact.email%>
<%= contact.telephone%>
<%= contact.source%>
<%= contact.company_id%>
</div>
<% end %>

Resources