render partial in content_for render not working - ruby

In a view I have
<% content_for :header do %>
<%= render #render %>
<% end %>
#render has value of 'intro'. The partial is not rendered. If I do:
<% content_for :header do %>
<%= render 'intro' %>
<% end %>
everything works great. Can someone explane why #render doesn't work?

Related

Tell <%= ... %> not to add a space

I use the following code in a template to render a date:
<% if job.end_month do %>
<%= "#{job.end_month}/" %>
<% end %>
<%= "#{job.end_year}" %>
This results in 3/ 2014 and not in 3/2014. How can I force Phoenix not to add a space after "#{job.end_month}/"?
One solution would be to create an external helper that did this for you:
e.g.
def job_string(%Job{end_month: nil}), do: job.end_year
def job_string(job), do: "#{job.end_month}/#{job.end_year}"
Then just use that in your view:
<%= job_string(job) %>
Alternatively if you don't want to do all that and would prefer a messier sort of look:
<% if job.end_month do %>
<%= "#{job.end_month}/#{job.end_year}" %>
<% else %>
<%= "#{job.end_year}" %>
<% end %>

cached votes not updating with validates_presence_of in model

I have acts_as_taggable installed and working for my products with cached votes.
After I tried to finish my validations inside the Product model with validates_presence_of the cached votes are not getting updated anymore.
Anyone had the same problem?
Any hint is appreciated.
Basic validation inside the product model. If this line is not there, the cached_votes work:
validates_presence_of :original_url, :format => URI::regexp(%w(http https))
How I trigger the votes:
<% if user_signed_in? %>
<% if current_user.liked? likeable %>
<%= form_tag unlike_path(likeable_type: likeable.class.to_s, likeable_id: likeable.id), method: :post, remote: true do %>
<% button_tag class: 'btn btn-block liked' do %>
<%= fa_icon 'heart-o' %> unlike
<% end %>
<% end %>
<% else %>
<%= form_tag like_path(likeable_type: likeable.class.to_s, likeable_id: likeable.id), remote: true do %>
<% button_tag class: 'btn btn-block' do %>
<%= fa_icon 'heart' %> like
<% end %>
<% end %>
<% end %>
<% else %>
<%= link_to new_user_registration_path do %>
<% button_tag class: 'btn btn-block' do %>
<%= fa_icon 'heart' %> like
<% end %>
<% end %>
<% end %>

Why isn't this simple elsif working in Rails/Ruby?

If there is a flash notice > "Flash notice"
If there isn't a flash notice and you're on the homepage > "Welcome"
Code:
<% if flash %>
<% flash.each do |name, msg| %>
<%= content_tag :span, msg, id: "flash_#{name}" %>
<% end %>
<% elsif current_page('/') %>
<% print 'Hi' %>
<% end %>
It prints the flashes correctly but not the welcome on the home page. Doesn't seem to matter if I try current_page or root_url or print 'Welcome' or just plain "Welcome" with no code wrapper. Why?
Yes, almost, I wasn't checking the flash properly at the beginning of the block. This works now:
<% if flash.present? %>
<% flash.each do |name, msg| %>
<%= content_tag :span, msg, id: "flash_#{name}" %>
<% end %>
<% elsif current_page?('/') %>
Welcome to The Spain Report.
<% end %>
Thank you all!

Bring code from view into a popup in Rails

I want to bring this code from my view html into a popup when a link is clicked
Is that possible in Ruby on Rails? I already have the pop up working but I'm wondering about the code to show just the comments:
<div class = "comments"><% if post.comments.exists? %>
<% post.comments.each do |comment| %>
<%= image_tag("http://www.gravatar.com/someavatarlink %) <!-- Retrieves Gravatar -->
<%= link_to comment.user.name, comment.user %>
<span class="timestamp"><%= time_ago_in_words(comment.created_at) %> ago</span>
<span class="content2"><%= comment.comment_content %></span>
<% end %>
<% end %></div>
Added Ajax call to _comment_form.html.erb
<%= link_to "Link", comment, :remote => true %>
Comments
<% end %></div></div>
<div id ="modal" class = "comments"><% if post.comments.exists? %>
<% post.comments.each do |comment| %>
<%= link_to comment.user.name, comment.user %>
<span class="timestamp"><%= time_ago_in_words(comment.created_at) %> ago</span>
<span class="content2"><%= comment.comment_content %></span>
<% end %>
<% end %></div>
Added def show into comments controller
class CommentsController < ApplicationController
def new
#post = post.new(params[:post])
end
def show
#comment = Comment.find(params[:id])
respond_to do |format|
format.js
end
def create
#post = post.find(params[:micropost_id])
#comment = Comment.new(params[:comment])
#comment.post = #post
#comment.user = current_user
if #comment.save
redirect_to(:back)
else
render 'shared/_comment_form'
end
end
end
Created show.erb.js and put it into 'comments' and 'shared' folders
$("#popup").html('<%= escape_javascript(render "comments") %>');
Then finally wrote my partial which is in comments/_comment.html.erb
<% if post.comments.exists? %>
<% post.comments.each do |comment| %>
<%= link_to comment.user.name, comment.user %>
<span class="timestamp"><%= time_ago_in_words(comment.created_at) %> ago</span>
<span class="content2"><%= comment.comment_content %></span>
<% end %>
<% end %>
1. Ajax call
To retrieve the data you use an Ajax call.
<%= link_to "Link", comment, :remote => true %>
Rails will evaluate these requests and will look for a .js view first (it will use .html if it does not exist).
Make also sure that the controller accepts requests to .js like
def show
#comment = Comment.find(params[:id])
respond_to do |format|
format.js
end
end
2. write js view
Add a show.erb.js view to your Comments . This is a JavaScript file with ERB evaluation.
In this template use your js popup code and tell it to fill a div with your html code like so:
$("#popup").html('<%= escape_javascript(render #comment) %>');
This will render the comment. The only thing we need then is a partial to render the html of the comment.
3. write partial for html
Write a partial for the view part you want to have in the popup. This can then be used in a normal html view or the js view. To make it work with the code above call it _comment.html.erb
To know more about partials you can check the guides here:
http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials

What is the correct way to pass multiple arguments to a ruby == operator?

I'm a noob trying to do this:
<% if #page[:title] == "Portraits" %>
<%= render :partial => "/shared/slideshow" %>
<% elsif #page[:title] == "Escapes" %>
<%= render :partial => "/shared/slideshow" %>
<% elsif #page[:title] == "Articulos pa Web" %>
<%= render :partial => "/shared/slideshow" %>
<% end %>
There has to be a concise way to do this, but I just can't figure it out.
Avoid putting the logic you currently have, in a view.
Put this in a helper method instead and use it in the view:
def get_partial_to_render
if ["Portraits","Escapes","Articulos pa Web"].include? #page[:title]
"shared/slideshow"
else
"some_other_template"
end
end
#Note that the partial should not have a leading `/` in the path to it.
And in your view:
<%= render :partial => get_partial_to_render %>
Or, if you do not want to render a partial if a name is not in the array:
def render_my_partial?
["Portraits","Escapes","Articulos pa Web"].include? #page[:title]
end
<%= render :partial => "shared/slideshow" if render_my_partial? %>
Note that the ? is part of the method name. Isn't Ruby wonderful? :D
<% if ["Portraits", "Escapes", "Articulos pa Web"].include?(#page[:title]) %>
<%= render :partial => "/shared/slideshow" %>
<% end %>

Resources