I'm making a random site to post gifs and images to for fun.
After I post something, the attributes show up under it, like this
[#Funpost id: 1, title: "Giphy Test", description: "HAHAHA SO FUNNY", url: "http://i.giphy.com/NuQ1Tz0fhqeEU.gif", created_at: "2015-01-20 00:21:34", updated_at: "2015-01-20 00:21:34">, #<Funpost id: 2, title: "test", description: "", url: "test", created_at: "2015-01-20 03:21:19", updated_at: "2015-01-20 03:21:19">
How do I get rid of it?
Thanks!!
You probably have an extra equals signs in your ERB code. e.g., if you do
<%= #funposts.each do |f| %>
<%= f.title %>
<%= f.description %>
<% end %>
You will end up displaying the funposts attributes as well as the title and description for each. However if you instead remove the equals sign from the first line
<% #funposts.each do |f| %>
<%= f.title %>
<%= f.description %>
<% end %>
You will only show the title and description for each funpost.
Related
I have a search_form_for and I need to be able to select multiple Payment statuses to make a Ransack search.
We already have an f.select dropdown box for this attribute, but we now need to be able to select more than one status at the same time.
form:
<%= search_form_for #search, url: admin_payments_path, html: {class: "form-inline"} do |f| %>
<div class="form-group">
<%= f.select :status_eq, payment_status_selector, { include_blank: "Payment status.."}, class: "form-control gray" %>
</div>
<% end %>
I've tried:
<%= f.select :status_eq, payment_status_selector, {include_blank: false}, {multiple: true, as: :radio_buttons} %>
gives me a select box with all the options allowing me to select multiple, but they are not radio_buttons and an empty value "" is passed along with the selected options.
<% payment_status_selector.each do |status| %>
<%= radio_button_tag :status_eq, "#{status[1]}", false, class: 'radio-checkbox' %>
<%= label_tag( "#{status[0]}") %>
<% end %>
This gives me a radio_button for each possible status, but I can't select more than one.
I can't figure out the correct way to do it. Maybe with check_boxes are a better option to be able to select/unselect statuses?
Yes, using check_box will be a better option.
Try,
<% payment_status_selector.each do |status| %>
<%= f.check_box :payment_statuses, { multiple: true }, status, false %>
<%= label status %>
<% end %>
And you can expect the values in controller from params as:
{ search: { payment_statuses: ["status 1", "status 2"] }
make sure that payment_status_selector returns array of values.
Hope that helped.
Reference: https://medium.com/programming-problem-solving-logbook/rails-multiple-checkboxes-e9c4c7fda356
I have a loop for comments that I want to print in a text_area_tag. Here is my code
<%=text_area_tag 'body', nil, rows: 10, cols: 25%>
<% #comments.each do |comment|%>
<p><%=comment.user_name%> says: <%=comment.comment%></p>
<%end%>
What I am trying to do is, instead of printing out all the comments in the p tags, I want to print them inside the text_area_tag
You can use the Rails helpers like simple_format
<% #comments.each do |comment| %>
<p><%=comment.user_name%> says: <%= simple_format comment.comment %></p>
<% end %>
Other helpers you may be wanting to experiment with: (text = comment.comment)
<%= raw text %>
<%= h text %>
<%= text.html_safe %>
I want to access a model in my application.html.erb file. For this I defined a before_filter in the application_controller.rb:
before_filter :populate_ages
protected
def populate_ages
#ages= Ages.all
end
In my application.html.erb I have the following:
<%= #ages.each do |age| %>
<p><%= age.name %></p>
<% end %>
The names are rendered correctly but I get additional output which is the array #ages. It looks like this:
[#<ageid: 1, name: "Herren", created_at: "2014-06-26 08:02:58", updated_at: "2014-06-26 08:02:58">, #<ageid: 2, name: "A-Jugend", created_at: "2014-06-26 08:02:58", updated_at: "2014-06-26 08:02:58">, #<ageid: 3, name: "B-Jugend", created_at: "2014-06-26 08:02:58", updated_at: "2014-06-26 08:02:58">] </ul>
I have this code in a navigation bar. So my navigation bar is extended by this additional text. Adding the loop in the body leads to the same output.
You don't want the = sign on the loop, change it to:
<% #ages.each do |age| %>
<p><%= age.name %></p>
<% end %>
By having <%= #ages ... you're telling rails to display the array
Just write as
<% #ages.each do |age| %>
<p><%= age.name %></p>
<% end %>
#ages is an Array instance. Now Array#each returns the receiver itself, when full iteration is completed. <%=.. %> means you are telling execute the each m,method and print the result of the #each method too, which is the problem, you have faced. Thus tell your template executed the method, don't print the result of it, and to do this correct syntax is <%.. %>.
I'm trying to include a variable in a title tag on a link.
e.g.
<%= link_to #user.facebook_link, title: "Facebook" %>
So it would look like so:
<%= link_to #user.facebook_link, title: "<% #user.name %> on Facebook", target: '_blank' do %>
How can I do this?
When using interpolation within a string in ruby, use #{...} instead:
<%= link_to #user.facebook_link, title: "#{#user.name} on Facebook", target: '_blank' do %>
I'm trying this:
<%= link_to 'Test', '#', {title: "this is my images tooltop message", class: "tooltip"} %>
Which returns the following HTML:
Test
When I remove the class attribute, I then get:
Test
How can I set both the title and class in the anchor tag so I get the following?
Test
You can do also:
<%= link_to 'Test', '#', :class => "tooltip", :title => 'this is my images tooltop message' %>
it's just which hash notation you prefer.
I'm useless! What I'm doing is in fact correct.
The tooltip jQuery plugin I'm using removes the title attribute - http://calebjacob.com/tooltipster/.
<%= link_to 'Show', '#', { :title => "this is my images tooltop message", :class => "tooltip"} %>
The older hash notation renders out the perfect HTML. I don't know why the new hash syntax results in this weird behavior though.
<%= link_to 'Test', '#', title: "this is my images tooltop message", class: "tooltip" %>