I am using simple_calendar in my Rails 5.0 application to link to events, everything works fine except that I get some extra data rendered in the calendar, I would like to get some help to figure it out.
This is what I have in the 'views/eventos/calendario.html.erb'
<h3>Eventos (<%= #eventos.count %>)</h3>
<%= month_calendar events: #eventos do |date, eventos| %>
<%= date.day %>
<% eventos.each do |evento| %>
<div>
<%= link_to evento.tipoEvento, evento %>
</div>
<% end %>
<% end %>
And this is what it is rendering for each day with programmed events:
<td class="day wday-5 past current-month has-events">
9
<div>
Entrenamiento
</div>
[#<Evento id: 4, fecha: "2016-12-09", tipoEvento:
"Entrenamiento", equipo_id: 11, comment: "Cancha 2",
created_at: "2016-12-08 06:07:19", updated_at: "2016-12-08
06:22:03", registrado: true>]
</td>
For days without events it is rendering the empty []. See, it is rendering the correct tag but I don't know how to avoid it rendering the object data, Can somebody help me please?
The solution is to replace a line in the generated view: views/simple_calendar/_month_calendar.html.erb
from:
<%= block.call day, sorted_events.fetch(day, []) %>
to:
<% block.call day, sorted_events.fetch(day, []) %>
So Jagdeep's answer was right, but in a different code line.
Related
Rails each do method is acting strangely and I do not know why.
controller
def index
#fabric_guides = FabricGuide.with_attached_image.all.order(:name)
end
index.html.erb
<div class="guide-items">
<%= #fabric_guides.each do |fabric| %>
<div class="guide-container">
<%= link_to fabric_guide_path(slug: fabric.slug) do %>
<%= image_tag fabric.image if fabric.image.attached? %>
<% end %>
<div class="guide-info">
<p class="g-name">
<%= link_to fabric.name,
fabric_guide_path(slug: fabric.slug) %>
</p>
</div>
</div>
<% end %>
</div>
I have two FabricGuide records so I expect two "guide-container" but I get three. Or more precisely I get two guide containers and a third block of text containing all the content from the last FabricGuide record.
I have almost an identical setup for articles and have never encountered this problem. I'd happily share more information if needed. Thank you!
Please remove = equal sign from your each loop of view code
like below :-
<% #fabric_guides.each do |fabric| %>
...
...
<% end %>
you have used this <%= #fabric_guides.each do |fabric| %> in your view that's why it shows all record in DOM.
The expression for erb tags is <% %>
now if we want to print that tag too then we apply <%= %>
Been trying to set up a regular blog in Ruby on Rails, and finally got the comment system to work within a post. However, when I loop through each comment and try to output the comment's title it displays this,
awef [#<Comment id: 6, title: "awef", link: nil, campaign_id: 5, user_id: 1, created_at: "2015-09-24 09:46:43", updated_at: "2015-09-24 09:46:43">]
Instead of just the title. How can I fix this?
The loop in your view should be something like this:
<% #blog.comments.each do |f| %>
<%= f.title %>
<% end %>
Please check you are using the right angular parenthesis (<%= %>) and not placing p puts or inspect commands inside them.
Edit:
Now that you show use the code, the problem is in the first angular parenthesis: should be <% not <%=. The first is for the logic, the latter to output erb code.
<% #blog.comments.each do |f| %> # remove "="
<%= f.title %>
<% end %>
<% ... %>: Executes the ruby code within the brackets
<%= ...%>: Executes the ruby code and show the executing result in webpage
Classic mistake, it's because you're using <%= ERB output tags for the .each loop. You can view more information on how to write correct ERB here.
You need to replace your <%= output tags with standard ERB tags <%:
<% #blog.comments.each do |f| %>
I was stung by that one myself when I was starting out.
The problem you have is probably the = in your each statement.
Try to use this:
<% #blog.comments.each do |f| %>
<%= f.title %>
<% end %>
Notice the removed = at <% #blog.comments.each do |f| %>
The reason is as the <%= %> always prints while <% %> only executes the code.
This question already has answers here:
What is the difference between <%, <%=, <%# and -%> in ERB in Rails?
(7 answers)
Closed 3 years ago.
I have an annoying problem where my view keeps displaying an array of my object's attributes! My submenu is suppose to show categories in a tree form, which works, but it also shows this annoying array!
[ <#Category id: 26, title: "subtest", description: "a test within a test, testception", created_at: "2015-03-01 03:15:29", updated_at: "2015-03-03 01:08:09", ancestry: "6/24">]
[ <#Category id: 24, title: "Test", description: "No not be alarmed, this is only a test.", created_at: "2015-03-01 02:06:35", updated_at: "2015-03-03 01:07:52", ancestry: "6">]
I definately don't want the user to see this. How do I get rid of it??
Show.html.erb view:
<div id="submenu">
<%= render 'submenu_cats', categories: #category.root.children %>
</div>
_submenu cats partial:
<ul>
<%= categories.each do |category| %>
<li>
<%= link_to_unless_current category.title, category_path %>
<%= render 'submenu_cats', categories: category.children if category.children.present? %>
</li>
<% end %>
Using: Rails 4.2, Ruby 2.1.5, Ancestry Gem
You are using <%= %> which means that you are outputting the results of a Ruby code, instead use <% %> to executes the ruby code within the brackets.
So change
<%= categories.each do |category| %>
To
<% categories.each do |category| %>
Hope this help. :)
Don't use...
<%= categories.each do |category| %>
Use
<% categories.each do |category| %>
When you use <%=, you're outputting the result of the expression. The result of categories.each is categories, the array that is being output. You don't want to output it, so use <% to evaluate the Ruby without outputting the results.
In my Rails app I'm trying to loop over the Submission instances inside my Folder instances with Rails templating code. It works. However, it's also returning each instance in code which doesn't seem to be JSON. It's what's returned when you look up an instance in the Rails console. Here's an example:
#<Submission id: 112, title: nil, content: nil, created_at: "2013-10-10 23:29:39", updated_at: "2013-10-10 23:29:39", user_id: 1, folder_id: 1, parent_id: nil>
Here's what the code looks like for the loop:
<%= #folder.submissions.each do |x| %>
<% if x.title != nil %>
<div id="<%= x.id %>" class="submission-textual">
<h1><%= x.title %></h1>
</div>
<% else %>
<% end %>
<% end %>
I checked my Folder and Submissions controllers but am not sure what this is. Why are these strings being rendered whenever I try and render an instance in my view? I'm still new to Ruby so that explains why I haven't seen this.
Try replacing the first line with
<% #folder.submissions.each do |x| %>
It's a small diffrerence, the equal sign after the first % was removed. I think that's what's causing the unwanted rendering.
The processing is as follows :
<% "ERB will evaluate this!" %>
<%= "ERB will evaluate and output this!" %>
Ok this is my first app in rails so hopefully this is a simple problem.
Here is my object:
- !ruby/object:ProductImage
attributes:
image_id:
product_id:
created_at:
updated_at:
attributes_cache: {}
This works:
<%= image_form.text_field :product_id %>
But I get undefined method `image_id' for:
<%= image_form.text_field :image_id %>
I just don't get it...
Cheers for any help on this.
This is the actual partial:
<div class="image">
<% new_or_existing = product_image.new_record? ? 'new' : 'existing' %>
<% prefix = "product[#{new_or_existing}_product_image_attributes][]" %>
<% fields_for prefix, product_image do |i| -%>
<div class="input select">
<%= i.text_field :image_id %>
<%= link_to_function "remove", "$(this).up('.image').remove()" %>
</div>
<% end -%>
</div>
P.S a text field is being used just as an example
Can you post the entire form? I have a hunch that maybe you did:
<% form_for :image do |image_form| %>
instead of:
<% form_for :product_image do |product_image_form| %>
Of course, I'd rethink displaying a form where people are manually entering id values in order to join an image to a product, but I understand you're in learning mode right now.
ID fields should not be text_fields, they should be checkboxes or drop-down select boxes. Why would your use know what the Rails ID is?