Can't print data "name" ruby on rails - ruby-on-rails

I am trying to print data from a object "root" to a table, everythink work fine but not printing this name i dont know why.
Code :
<% root = NameOfDatabase.find_by_d_id(d.id) %>
<% logger.info root.inspect %>
<% if root.nil? %>
<td class="col-4"> <% "-" %> </td>
<% else %>
<td class="col-4"> <% root.name %> </td>
<% end %>
Picture: View
Log file :
...
#<Name**** id: ****, d_id: ****, name: "Alice", dd_id: ****>
Completed in 99ms (View: 7, DB: 33) | 200 OK [https://*********/dd/search_ajax?option=active&query=s001]
As following in root we have data i want only print root.name in table !!
thanks you

You need to use <%= %> instead of <% %>
<% %> will just evaluate the expression, it won't output the result to ERB template
<%= %> will evaluate the expression and outputs to ERB template.
Try the below:
<% root = NameOfDatabase.find_by_student_id(student.id) %>
<% logger.info root.inspect %>
<% if root.nil? %>
<td class="col-4"> <%= "-" %> </td>
<% else %>
<td class="col-4"> <%= root.name %> </td>
<% end %>
Reference:
What is the difference between <%, <%=, <%# and -%> in ERB in Rails?

Related

How to place two elements in a row while using .each loop in rails

I want to show two columns of the code in table data tag"" per row while using '.each' method. But the issue is that the following code displays one column in a row.
<table>
<% #lease.apartment.roommates.each do |roommate| %>
<tr>
<td colspan="5">
<% unless roommate == #lease.second_occupant || roommate == #lease.user %>
<% if roommate.current_room.present? %>
<p>
<%= roommate.full_name %> -
<% if roommate.current_room.apartment == #lease.apartment%>
<%= roommate.current_room&.label %>
<% end %>
<br>Email:<%= roommate.email %><br>Phone:<%= roommate.phone %><br>
<% if #lease.end_at.present? %>
Lease End date (if applicable):<%= #lease.end_at %>
<% end %>
</p>
<% end %>
<% end %>
</td>
</tr>
<% end %>
</table>
You can use the each_slice method for dividing the array into slices of 2 elements each:
<table>
<% #roommates.each_slice(2) do |roommate_slice| %>
<tr>
<td colspan="5">
<%= roommate_slice[0].full_name %>
<%= roommate_slice[1].full_name %>
</td>
</tr>
<% end %>
</table>

Best way to loop through array

My controller passes this array to the view:
#ipd = Ipdw.connection.select_all("select * from [My Preferences]")
This is my view code:
<h1>Hello, Rails</h1>
<body>
<% #ipd.each do |ip| %>
<p><%= ip %></p>
<% end %>
</body>
This is displayed in the browser:
{"Pref ID"=>1, "Source ID"=>1, "Username"=>"RS2559", "Field Name"=>"CATEGORY", "String Value"=>"IP Placemat Deliverables", "Integer Value"=>nil, "Decimal Value"=>nil, "Created Dt Tm"=>2009-08-10 03:01:36 UTC, "Update Dt Tm"=>2009-12-14 16:04:01 UTC}
{"Pref ID"=>2, "Source ID"=>1, "Username"=>"RS2559", "Field Name"=>"TYPE", "String Value"=>nil, "Integer Value"=>nil, "Decimal Value"=>nil, "Created Dt Tm"=>2009-08-10 03:01:40 UTC, "Update Dt Tm"=>2009-12-14 16:04:01 UTC}
...
I want to show the "Username" and "Field Name" in a table. How can I loop through the array to show only these columns in a readable matter?
It looks like your query is returning an array of hashes. Just use the keys to pick the values you are interested in:
<% #ipd.each do |ip| %>
<p>
<%= ip['Username'] %><br />
<%= ip['Field Name'] %>
</p>
<% end %>
How about something like:
<h1>Hello, Rails</h1>
<body>
<% #ipd.each do |ip| %>
<p>
<% ["Username", "Field Name"].each do |k| %>
<%= ip[k] %>
<% end %>
</p>
<% end %>
</body>
That, naturally, will just put the values on your page. You could do other things like build a table or make bullet points or whatever.
That might look something like:
<h1>Hello, Rails</h1>
<body>
<table>
<tr>
<% #field_names.each do |field_name| %>
<th>
<%= field_name %>
</th>
<% end %>
</tr>
<% #ipd.each do |ip| %>
<tr>
<% #field_names.each do |field_name| %>
<td>
<%= ip[field_name] %>
</td>
<% end %>
</tr>
<% end %>
</table>
</body>
This assumes in your controller you did something like:
#field_names = ["Username", "Field Name"]
That way, you can decide in your controller which fields you want (and in what order) and not have to futz with your html.erb file.
If you wanted to pretty up those label names a little, in your controller you could do:
#label_mapping = {"Username"=>"User Name", "Field Name"=>"Field Name"}
And then:
<h1>Hello, Rails</h1>
<body>
<table>
<tr>
<% #field_names.each do |field_name| %>
<th>
<%= label_mapping[field_name] %>
</th>
<% end %>
</tr>
<% #ipd.each do |ip| %>
<tr>
<% #field_names.each do |field_name| %>
<td>
<%= ip[field_name] %>
</td>
<% end %>
</tr>
<% end %>
</table>
</body>
There are maybe 2 ways:
First you have a model for this kind of data:
<% #ipd.each do |ip| %>
<p><%= ip.username %></p>
<% end %>
Secound:
Your Dataset you get from your external Database looks like a hash. So try something like this:
<% #ipd.each do |ip| %>
<p><%= ip["username"] %></p>
<% end %>
I think kind 2 is what you need ;).
In a view i wouldn't use a shorter way of looping, be cause the code get less readable with something like this: abc.each{|letter| "<li>#{letter}</li>"}

If else Statement in <%= raw {} %> ERB

I have something like this:
<%= raw query.inline_columns.map {|column|
"<td class=\"#{column.css_classes}\">#{column_content(column, issue)} </td>"
}.join %>
I want to add a if else Statement when the css.classes == "assigned_to" how can i do that ?
I tried this but don't work
<%= raw query.inline_columns.map {|column|
<% if #{column.css_classes} == "assigned_to" %>
"<td class=\"#{column.css_classes}\">#{column_content(column, issue)} </td>"
<% else %>
"<td class=\"#{column.css_classes}\">#{column_content(column, issue)} TEST TEST </td>"
<% end %>
}.join %>
I'm a beginner in ERB language sorry.
Thank's for help
Do you need the final result to be one string? Because if not, it should be simpler to just return the html for each inline_columns, like:
<% query.inline_columns.each do|column| %>
<% if column.css_classes == "assigned_to" %>
<td class="<%= column.css_classes %>"> <%= column_content(column, issue) %> </td>
<% else %>
<td class="<%= column.css_classes %>"> <%= column_content(column, issue) %> TEST TEST </td>
<% end %>
<% end %>

Rails anyway to add an if statement to a loop

In my rails application I have a set of attachments that display, with a main image being one of them. However, I would like to loop through all of them except the main attachment. My view code right now is:
<% if #asset.attachments.size > 0 %>
<table>
<tr>
<% #asset.attachments.each_with_index do |attachment, i| %>
<% if i%5 == 0 %>
</tr><tr>
<%end%>
<td style="width: 150px;" valign="bottom" align="center">
<%= image_tag(attachment.attachment.url(:thumb)) if attachment.is_image? %>
<%= image_tag("/images/Excel.png") if attachment.is_excel? %>
<%= image_tag("/images/Word.png") if attachment.is_word?%>
<br />
<%= link_to attachment.name.to_s,attachment.attachment.to_s %>
</td>
<%end%>
</tr>
</table>
<%end%>
but, I would like to add something like if !main_image to this line of code:
<% #asset.attachments.each_with_index do |attachment, i| %>
I don't know if that is possible though.
This answer is in continuation to answer on this question
Add another method to your model, which return you the attachments which are not main image.
scope :not_main_image, where(:main_image => false)
On a side note, you might want to move all this logic into a helper method:
<%= image_tag(attachment.attachment.url(:thumb)) if attachment.is_image? %>
<%= image_tag("/images/Excel.png") if attachment.is_excel? %>
<%= image_tag("/images/Word.png") if attachment.is_word?%>
<br />
<%= link_to attachment.name.to_s,attachment.attachment.to_s>
Say you create a helper method like:
def link_to_attachment attachment
html = ""
html += image_tag(attachment.attachment.url(:thumb)) if attachment.is_image?
html += image_tag("/images/Excel.png") if attachment.is_excel?
html += image_tag("/images/Word.png") if attachment.is_word?
html += "<br />"
html += link_to(attachment.name.to_s, attachment.attachment.to_s)
html.html_safe
end
Then in you view you can replace it with:
<td style="width: 150px;" valign="bottom" align="center">
<%= link_to_attachment attachment %>
</td>
<% #asset.attachments.each_with_index do |attachment, i| %>
&lt% next if attachment.main_image %>
It depends on how you define the main attachment
<% #asset.attachments.each_index do |attachment, i| %>
<% unless attachment.main? %>
...
<% end %>
<% end %>
OR
<% #asset.attachments.each_index do |attachment, i| %>
<% if attachment.main? %>
<% next %>
<% else %>
...
<% end %>
<% end %>
OR put this in your controller
#attachments = #asset.attachments.where(:main => false)
after that, iterate over #attachments instead of #asset.attachments

html5 in rails3 views

I have a view with some embedded ruby in it... i want to insert a cell <td></td> into it, but when i do that it gives several error messages? This is because i concatenate a text field and a submit button into the embedded ruby.
This is my code:
<table>
<% for answer in #question.answers %>
<tr>
<!-- this displays all the possible answers -->
<td>
<%= answer.text %>
</td>
<% if current_user.can_vote_on? (#question) %> <!-- if a current user has not yet voted.. -->
<td> <%= form_tag('/vote', :method => "post") do
hidden_field_tag('vote[answer_id]', answer.id) +
submit_tag("Vote")
end %> <!-- vote button.. -->
<% end %>
</td>
</tr>
<% end %>
<% if current_user.can_vote_on? (#question) %> <!-- this shows a answer text field -->
<tr>
<td>
<%= form_tag('/vote/new_answer', :method => "post") do
hidden_field_tag('answer[question_id]', #question.id) +
hidden_field_tag('answer[user_id]', current_user.id) +
text_field_tag('answer[text]') + <!-- in here i want an extra </td><td> tag -->
submit_tag('Vote')
end %>
</td>
</tr>
<% end %>
My question is: how can i exit the embedded ruby and at the same time staying in the concatenated string...? I want to add the </td> and the <td> after the text_field_tag('answer[text]')
I tried this:
<td>
<%= form_tag('/vote/new_answer', :method => "post") do %>
<%= hidden_field_tag('answer[question_id]', #question.id) %>
<%= hidden_field_tag('answer[user_id]', current_user.id) %>
<%= text_field_tag('answer[text]') %>
</td>
<td>
<%= submit_tag('Vote') %>
<% end %>
</td>
And it works!
Thijs
Simple answer: It's not possible.
I suggest you try a different approach such as using divs inside your td elements. If I were you I wouldn't concatinate the strings together.
<%= form_tag('/vote/new_answer', :method => "post") do %>
<%= hidden_field_tag(answer[question_id], #question.id %>
... so on ...
<div class="position_it_somewhere_with_this_class"><%= submit_tag("vote") %></div>
<% end %>
You don't concatenate tags!
Also you don't use divs in table rows. put the classes in your tds
...
<%= form_tag('/vote/new_answer', :method => "post") do %>
<%= hidden_field_tag('answer[question_id]', #question.id) %>
<%= hidden_field_tag('answer[user_id]', current_user.id) %>
<%= text_field_tag('answer[text]') %>
<%= submit_tag('Vote') %>
<% end %>
</td>
</tr>
..

Resources