Add interval text if previous element exists - ruby-on-rails

Using Rails 3. This is a front-end design question.
Goal:
Contact | Email | URL
show.html.erb:
<% if !#shop.contact.blank? %>
<%= #shop.contact %>
<% end %>
<% if !#shop.email.blank? %>
<%= #shop.email %>
<% end %>
<% if !#shop.url.blank? %>
<%= link_to #shop.url, #shop.url, :target => "_blank" %>
<% end %>
How do I put in | only when the previous and after element has values? At current stage, if there is no value, nothing is output.
Many thanks.

<% url = link_to(#shop.url, #shop.url, :target => "_blank") if #shop.url.present? %>
<%= [#shop.contact, #shop.email, url].select(&:present?).join(" | ") %>
This creates an array of all your elements, selects those which have a value (as present? is the opposite of blank?) and then joins each element of the remaining array by putting a pipe between them.
That said, if you have more complex logic, you should probably create a helper method. The ERB templates are not a good place for complex logic. This above is bordering acceptable.

Related

How to handle the checkbox with two array and if in one array user on-click the checkbox that should append in second array

#complete[] and #incomplete[] are two array then I need to used with these array with checkbox complete array is by default true and incomplete by default false .
<%= form_with(model: #task, local: true) do |form| %>
<% #complete = Array.new %>
<% #incomplete = Array.new %>
<% #tasks.each do |task| %>
<% if task.complete != false then %>
<% #complete << task.name %>
<% else %>
<% #incomplete << task.name %>
<% end %>
<% end %>
<!-- complete task -->
<%= #complete %>
<!-- incomplete task -->
<%= #incomplete %><br>
<% #complete.each do |i| %><div id = "task_check">
<%= form.check_boX "#incomplete[]", incomplete.id %>
<%= form.check_box "chkbox_ary[#{i}]" , {checked:true} %>
<%= form.submit "update"%>
<%= content_tag(:strike, i)%>
<br></div>
<% end %>
<br> <hr>
<script>
<% #incomplete.each do |i| %>
<%= form.check_box "chkbox_ary[#{i}]" %>
<%= form.submit "update"%>
<%= i %><br>
<% end %>
<% end %>
If I check the box as false in complete then it should append to array called complete and if I check the box true in incomplete then it should append to second array called complete.
if I may, I'd like to point out some stylistic issues in your code first:
it is unusual to compare with false, i.e., if task.complete != false should be just if task.complete this works thanks to the concept of Truthiness in Ruby
you can just leave off the then in an if-conditional, that's what most Ruby developers do, i.e., if task.complete then can just be if task.complete
view templates (.erb files) should contain as little logic as possible, i.e., extract the creation of the #incomplete and #complete arrays into your controller or some other place
Ruby Enumerable has many methods to make common work easier, for instance splitting a collection into two collections can be done with partition, i.e., you can do #complete, #incomplete = #tasks.partition(&:complete) to populate the complete and incomplete arrays (&:complete is a shorthand for { |task| task.complete }, google Ruby symbol to proc if you want to know more about it)
I'm not sure I understand your question as it is. At the time the complete and incomplete arrays exist no user interaction takes place. I.e., nobody can click anything. When the HTML is rendered, no arrays exist anymore.
So there's two ways I can interpret your question:
You wish to move the DOM elements around => you need to use JavaScript for that
You wish to partition the checkboxes when the form is submitted => you need to show us the code in your controller that handles the form-submit for us to help you out
Which is it?
PS: I know this isn't an answer right now, but I could not write this as a comment since it's too long and a comment would have been illegible.

Rails attribute on manual array

In my controller I have:
#payment_types = [['Cash' ,'1'], ['Card', '2']]
What I'm trying to achieve is to show in view Cash and Card while writing on database 1 and 2.
In my view I tried:
<% payment_types.each do |payment_type| %>
<%= payment_type %>
<% end %>
which shows ['Cash' ,'1'] ['Card', '2']]
How can I show instead in my view Cash or Card?
I'm not sure if I understand your question, but if you want to show only 'Cash', and 'Card', you can do it by passing another argument (responsible for hash value, I called it _ because it's a convention for unused arguments) to your block, like this:
<% payment_types.each do |payment_type, _| %>
<%= payment_type %>
<% end %>
You could also do it like this
<% payment_types.each do |payment_type| %>
<%= payment_type.first %>
<% end %>

Dynamically displaying the column values

Here, I have 10 columns i.e., answer1, answer2, answer3, ..., answer10 in the table MgAnswer.
I have to check whether each column value is present or not. Only if it present,then I have to display it in the page.
Im giving column names dynamically within for loop
<% (1..10).each do |i| %>
<% if MgAnswer."answer#{i}".present? %>
<%= MgAnswer."answer#{i}" %>
<% end %>
<% end %>
Im ending up with Syntax error.
You can indeed dynamically invoke methods in ruby, but this is not the syntax. Instead do
<% (1..10).each do |i| %>
<% if MgAnswer.public_send("answer#{i}").present? %>
<%= MgAnswer.public_send("answer#{i}") %>
<% end %>
<% end %>
It should seem like the following:
<% (1..10).each do |i| %>
<%= MgAnswer.send("answer#{i}") %>
<% end %>
Since ruby can't evaluate line as MgAnswer."method". Also you can just skip if condition, because it will be evaluated to empty string "".

Rails: Find the ID of a record after using exists?

Using two different models, RecordLabel and Artist, I want to link to their pages if the record is found using their usernames. I have no problems finding if the record exists, but I can't figure out how to find the ID of that record. What I have:
<% if RecordLabel.exists?(:username => "#{#artist.artist_profile.record_label_name}") %>
<%= link_to #artist.artist_profile.record_label_name, record_label_path(RecordLabel.find(### NEED RECORD LABEL ID ###) %>
<% else %>
<%= #artist.artist_profile.record_label_name %>
<% end %>
You can get the record very easily this way (if it exists):
RecordLabel.where(:username => "#{#artist.artist_profile.record_label_name}").first
So, your code becomes:
<% if RecordLabel.exists?(:username => "#{#artist.artist_profile.record_label_name}") %>
<%= link_to #artist.artist_profile.record_label_name,
record_label_path(RecordLabel.where(:username => "#{#artist.artist_profile.record_label_name}").first) %>
<% else %>
<%= #artist.artist_profile.record_label_name %>
<% end %>
This should work and solve your problem.

Ruby on Rails: How to print contents of variable in view and make checkbox?

I am trying to create a checklist in rails using form_for. This checklist is taken from a table which I gained in the create action of my sign_ins controller:
#content = OrientationContent.where(site_id: session[:site_id])
In my view I want to use the form_for helper to iterate through the list in #content:
<%= form_for(:sign_ups ) do |f| %>
<% #content.each do |c| %>
<%= f.check_box nil %> <%= c %> <br>
<% end %>
<% end %>
However this is not working and it produces two square brackets on the page: [].
How do I go through the list and print the name while creating a check box on the left of it? The check box does not have any meaning or data, I just need it present for reference.
Solved:
In the controller, need to pluck an individual field:
#content = OrientationContent.where(site_id: 1).pluck(:content)
In the view, structure as so:
<%= form_for(:sign_ups) do |f| %>
<% #content.each do |c| %>
<%= f.check_box nil %> <%= c %> <br>
<% end %>
<% end %>

Resources