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

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 %>

Related

render partial in content_for render not working

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?

undefined local variable or method `comment' for #<#<Class:0xb6d2fd0>:0xc077dd8>

undefined local variable or method `comment' for #<#<Class:0xb6d2fd0>:0xc077dd8>
This is what my _comment.html.erb partial looks like
<div class="media">
<%= link_to '#', class: 'pull-left' do %>
<%= image_tag(comment.user.avatar.small.url) if comment.user.avatar? %>
<% end %>
<div class="media-body">
<small>
<%= comment.user.name %> commented <%= time_ago_in_words(comment.created_at) %> ago
<% if policy(comment).destroy? %>
| <%= link_to "Delete", [#topic, #post, comment], method: :delete %>
<% end %>
</small>
</div>
The create section of my comments_controller.rb
def create
#post = Post.find(params[:post_id])
#comment = current_user.comments.build(comment_params)
#comment.post = #post
authorize #comment
if #comment.save
redirect_to #post, notice: "Comment was saved successfully."
else
flash[:error] = "Error creating comment. Please try again."
render :new
end
end
and how I rendered in my posts#show view
<div>
<%= render :partial => "comments/comment", local: {comment: #comment} %>
</div>
You're not passing any locals to this partial:
<%= render :partial => "comments/comment" %>
You'd want to do this:
<%= render :partial => "comments/comment", :locals => { :comment => #comment } %>
Or use the shortcut:
<%= render 'comments/comment', :comment => #comment %>
You're declaring a local variable named comment in your _comment.html.erb partial.
You'll need to pass the #comment from your controller like this:
<%= render :partial => "comments/comment", locals: { comment: #comment } %>
Take a look at Layouts and Rendering Rails Guide for more info.

skipping(going straight to the show page) the index page if there is only 1 item in the database

How can I skip the index page and go straight to the show page if there is exactly only 1 project update in my database for that project(while also making sure no button gets displayed if there is no update(zero) in the database?
I tried this:
<% if #project.updates.any? %>
<%= button_tag type: "button", :class => "radius" do %>
<% if #project.updates=1 %>
<%= link_to 'Project Update', project_update_path(#project), :style => "color: white" %>
<% else %>
<%= link_to 'Project Updates', all_project_updates_path(#project), :style => "color: white" %>
<% end %>
<% end %>
<% end %>
but i get this error:
undefined method `each' for 1:Fixnum
On this line:
<% if #project.updates=1 %>
What is the proper syntax for this?
Below is the relevant code:
My button:
<% if #project.updates.any? %>
<%= button_tag type: "button", :class => "radius" do %>
<%= link_to 'Project Updates', all_project_updates_path(#project), :style => "color: white" %>
<% end %>
<% end %>
These is my custom route:
get 'all_project_updates/:id' => 'project_updates#index', as: 'all_project_updates'
These are the final generated routes:
project_updates_path GET /project_updates(.:format) project_updates#index
project_update_path GET /project_updates/:id(.:format) project_updates#show
This is my projects controller(show action)
def show
#project = Project.find(params[:id])
#comments = Comment.all.where(:project_id => #project.id)
#updates = ProjectUpdate.all.where(:project_id => #project.id)
end
And this is my project updates controller index action:
def index
#projectUpdates = ProjectUpdate.where(:project_id => params[:id])
respond_to do |format|
format.html
end
end
And this is my project updates controller show action:
def show
#projectUpdate = ProjectUpdate.find(params[:id])
respond_to do |format|
format.html
end
end
You probably meant:
<% if #project.updates.count == 1 %>
== is for comparison, = is usually for assignment. Also, you need to compare updatesnumber to1(you can get number withcountmethod), notupdates` themselves.
Instead of comparison, you can use Enumerable#one? method:
<% if #project.updates.one? %>

Like button does not update with AJAX

I am passing a collection (#feed_items) to a _feed_item partial via the :collection option and converting it to dailypost with :as => :dailypost.
Inside the _feed_item partial I rendered another partial for _like_button, and i used :locals to continue using dailypost.
Everything works fine with the database. Likes get added and taken out :) BUT
I am trying to use (AJAX) to create.js.erb & destroy.js.erb the like button.
For some reason only the top post gets updated correctly and i have to refresh the page to see the ones below it.
I know the solution involves assigning a unique post id to each "like" element, say "like-123", but that is where i am stuck.......
I also know that the problem may be in _feed_items.html.erb because i am passing two ids....any suggestions??
Views
_feed.html.erb
<% if #feed_items.any? %>
<ol>
<%= render partial: 'shared/feed_item', collection: #feed_items, :as => :dailypost %>
</ol>
<% end %>
_feed_items.html.erb
<li id="<%= #dailypost.id %>">
<span class="user">
<%= link_to dailypost.user.name, dailypost.user %>
<span class="content"><%= dailypost.content_html %></span>
<div id="like">
<%= render :partial => 'likes/like_button', :locals =>{:dailypost => dailypost} %>
</div>
</li>
_like_button.html.erb
<% if like = current_user.likes.find_by_dailypost_id(dailypost.id) %>
<%= form_for like, :html => { :method => :delete }, :remote => true do |f| %>
<%= f.submit "Unlike" %>
<% end %>
<% else %>
<%= form_for current_user.likes.build, :remote => true do |f| %>
<div><%= f.hidden_field :dailypost_id, value: dailypost.id %></div>
<%= f.hidden_field :user_id %>
<%= f.submit "Like" %>
<% end %>
<% end %>
create.js.erb
$("#like").html("<%= escape_javascript(render :partial => 'like_button', :locals => {:dailypost => #dailypost}) %>");
destroy.js.erb
$("#like").html("<%= escape_javascript(render :partial => 'like_button', :locals => {:dailypost => #dailypost}) %>");
Controller
class LikesController < ApplicationController
respond_to :html, :js
def create
#like = Like.create(params[:like])
#dailypost = #like.dailypost
respond_to do |format|
format.js
format.html { redirect_to :back }
end
end
def destroy
like = Like.find(params[:id]).destroy
#dailypost = like.dailypost
respond_to do |format|
format.js
format.html { redirect_to :back }
end
end
end
Hmmm.Replace the first-line in _feed_items.html.erb with this.
<li id="dailypost<%= dailypost.id %>">
and In create.js.erb and destroy.js.erb, make changes
$("#like").html(...)
To
$("#dailypost<%= dailypost.id%> #like").html(...)
This should Work.

Sinatra Partial with data?

I am making a super small Sinatra blog application, how could I take entries from a database, format them, and insert them into my layout?
class Blog < Sinatra::Base
helpers do
def partial (template, locals = {})
erb(template, :layout => false, :locals => locals)
end
end
get "/list" do
#posts = Post.all
erb :list
end
end
list.erb:
<% #posts.each do |post| %>
<%= partial(:post, :post => post) %>
<% end %>
post.erb:
<h1><%= post.title %></h1>
<p><%= post.body %></p>
<% #posts.each do |post| %>
<%= erb :"_partial_name", :locals => {} %>
<% end %>
the partial template need start with _

Resources