Getting multiple buttons for the same action - ruby-on-rails

I have a blog rails app where users can comment on an article and the article author can message the user directly from the comment for which i have added a send message button, i am using mailbox for getting the message option, everything is working but the problem is when there are multiple comments the message button also get multiplied, but i don't want that what i am trying to have is one message button for each comment.
This is what am getting right now:
my code
<div id="comments">
<h2 class="comment_count">
<%= pluralize(#shipment.comments.count, "Bid") %>
</h2>
<% #comments.each do |comment| %>
<div class="comment">
<li class="round-image-50"><%= image_tag(current_user.avatar.url(:thumb)) %></li><h6 class="username"><strong><font style="text-transform: capitalize;"><%= comment.user.full_name %></strong></font></h6>
<div class="shipment">
<p class="content">
<div class="text-center left col-md-4">
<%= comment.content %>
</div>
<% if #shipment.user == current_user %>
<% #shipment.comments.each do |comment| %>
<%= link_to 'Send Message', new_conversation_path(recipient_id: comment.user_id), class: "btn btn-default btn-xs" %>
<% end %>
<% end %>
</p>
</div>
</div>
<% end %>
<%= render "comments/form" %>
</div>

Change this:
<% if #shipment.user == current_user %>
<% #shipment.comments.each do |comment| %>
<%= link_to 'Send Message', new_conversation_path(recipient_id: comment.user_id), class: "btn btn-default btn-xs" %>
<% end %>
<% end %>
to
<% if #shipment.user == current_user %>
<%= link_to 'Send Message', new_conversation_path(recipient_id: comment.user_id), class: "btn btn-default btn-xs" %>
<% end %>
PS: I assumed that #comments is same as #shipment.comments from your shown code

Related

Rails Syntax Error: expecting keyword end

I get this error: app/views/topics/show.html.erb:49: syntax error, unexpected keyword_ensure, expecting keyword_end. I don't have a line 49. I checked to see if I was missing any <% end %> and I find that I don't.
<h1><%= #topic.name %></h1>
<% if user_is_authorized_for_topics? || if user.moderator? %>
<%= link_to "Edit Topic", edit_topic_path, class: 'btn btn-success' %>
<% end %>
<% if user_is_authorized_for_topics?%>
<%= link_to "Delete Topic", #topic, method: :delete, class: 'btn btn-danger', data: { confirm: 'Are you sure you want to delete this topic?' } %>
<% end %>
<div class="row">
<div class="col-md-8">
<p class="lead"><%= #topic.description %></p>
<% #topic.posts.each do |post| %>
<div class="media">
<div class="media-body">
<h4 class="media-heading">
<%= link_to post.title, topic_post_path(#topic, post) %>
</h4>
<small>
submitted <%= time_ago_in_words(post.created_at) %> ago by <%= post.user.name %> <br>
<%= post.comments.count %> Comments
</small>
</div>
</div>
<% end %>
<% #topic.sponsored_posts.each do |sponsored_post| %>
<div class="media">
<div class="media-body">
<h4 class="media-heading">
<%= link_to sponsored_post.title, topic_sponsored_post_path(#topic, sponsored_post) %>
</h4>
<small>
submitted <%= time_ago_in_words(sponsored_post.created_at) %> ago <br>
</small>
</div>
</div>
<% end %>
</div>
<% if current_user %>
<div class="col-md-4">
<%= link_to "New Post", new_topic_post_path(#topic), class: 'btn btn-success' %>
</div>
<% end %>
</div>
Probably this line:
<% if user_is_authorized_for_topics? || if user.moderator? %>
You only need if once, like this:
<% if user_is_authorized_for_topics? || user.moderator? %>

Render only when i set it to render

so my question is how can i enable this piece of code only when i want? I have multiple pages and some pages i need to remove that piece of code.
This is my current partial _navbar.html.erb and div.spotlight is the piece of code i need to disable in some pages
<div class="container">
<nav class="logonav">
<div class="row">
<div class="col50"><span class="logo"></span>
<h2>Musicus</h2>
<p>De ti para o mundo</p>
</div>
<% if user_signed_in? %>
<div class="col50">
<%= link_to 'Logout', destroy_user_session_path, class: 'ui-btn btn-normal', method: :delete %>
</div>
<% else %>
<div class="col50">
<%= link_to new_user_registration_path, class: 'ui-btn btn-accent' do %>
<span class="icon-add-user"></span> Criar Conta
<% end %>
<%= link_to new_user_session_path, class: 'ui-btn btn-normal' do %>
<span class="icon-login"></span> Login
<% end %>
</div>
<% end %>
</div>
</nav>
<div class="spotlight">
<%= yield :other_message %>
<%= render 'search' %>
<%= yield :primary_message %>
</div>
<%= yield :other_nav %>
</div>
You can use a simple if statement:
<% if show_spotlight %>
<div class="spotlight">
<%= yield :other_message %>
<%= render 'search' %>
<%= yield :primary_message %>
</div>
<% end %>

Rails NoMethodError in Albums#show

I'm trying to show a link to a new description if an album doesn't currently have a description, but I keep getting a NoMethodError:
<% if #album.description.size = 0 %>
<div class="new-description"><%= link_to 'Add Description', "/albums/#{x.id}/edit" %></div>
<% else %>
<div class="description"><%= #album.description %></div>
<% end %>
This should also work, provided description is supposed to be a string.
<% if not #album.description.nil? %>
<div class="new-description"><%= link_to 'Add Description', "/albums/#{x.id}/edit" %></div>
<% else %>
<div class="description"><%= #album.description %></div>
<% end %>
Have you tried:
<% if #album.description.blank? %>
<div class="new-description"><%= link_to 'Add Description', edit_album_path(#album) %></div>
<% else %>
<div class="description"><%= #album.description %></div>
<% end %>
Try using Object#try: https://github.com/rails/rails/blob/be589a8b01500a4b52a86248458bf57597e568e8/activesupport/lib/active_support/core_ext/object/try.rb
<% if #album.try(:description) %>
<div class="description"><%= #album.description %></div>
<% else %>
<div class="new-description"><%= link_to 'Add Description', "/albums/#{x.id}/edit" %></div>
<% end %>

Mailboxer gem, "undefined method `participants' for nil:NilClass"

#conversation doesn't seem to load the actual conversation. I get a nilClass error when loading an individual conversation and I'm not sure why. I feel like I'm missing something silly.
Here is the error (which persists to any use of conversation in the show view):
undefined method `participants' for nil:NilClass
Extracted source (around line #1):
<% conversation.participants.each do |participant| %>
<% unless participant == current_user %>
<%= gravatar_for participant %>
<% end %>
<% end %>
Rails.root: /home/alex/workspace/meetexplore_app
Application Trace | Framework Trace | Full Trace
app/views/conversations/_participants.html.erb:1:in `_app_views_conversations__participants_html_erb___865731980245228929_7035 9899418680'
app/views/conversations/show.html.erb:4:in `_app_views_conversations_show_html_erb__775537665966091139_41963900'
My conversations/show.html.erb file:
<% page_header "Conversation" %>
<p>Chatting with
<%= render 'conversations/participants', conversation: #conversation %>
</p>
<div class="panel panel-default">
<div class="panel-heading"><%= #conversation.subject %></div>
<div class="panel-body">
<div class="messages">
<% #conversation.receipts_for(current_user).each do |receipt| %>
<div class="media">
<% message = receipt.message %>
<div class="media-left">
<%= gravatar_for message.sender, 45, message.sender.name %>
</div>
<div class="media-body">
<h6 class="media-heading"><%= message.sender.name %>
says at <%= message.created_at.strftime("%-d %B %Y, %H:%M:%S") %></h6>
<%= message.body %>
</div>
</div>
<% end %>
</div>
</div>
</div>`
My _participants.html.erb partial:
<% conversation.participants.each do |participant| %>
<% unless participant == current_user %>
<%= gravatar_for participant %>
<% end %>
<% end %>
Yet this index works:
<% page_header "Your Conversations" %>
<p><%= link_to 'Start conversation', new_message_path, class: 'btn btn-lg btn-primary' %></p>
<ul class="list-group">
<%= render partial: 'conversations/conversation', collection: #conversations %>
</ul>
<%= will_paginate %>
_conversation.html.erb partial:
<li class="list-group-item clearfix">
<%= link_to conversation.subject, conversation_path(conversation) %>
<p><%= render 'conversations/participants', conversation: conversation %></p>
<p><%= conversation.last_message.body %>
<small>(<span class="text-muted"><%= conversation.last_message.created_at.strftime("%-d %B %Y, %H:%M:%S") %></span>)</small></p>
</li>
So I was missing the before_action and function get_conversation to actually find the conversation with the proper id... woops!

List of voted pins per user

I have used the acts as votable gem to like pins. Now I want a view (my favorites) with all the pins a user has liked. I get stuck in making this happen.
If have this in my pins controller file:
def my_favorites
#pins = current_user.pins
end
def like
#pin.liked_by current_user
redirect_to :back
end
def unlike
#pin.unliked_by current_user
redirect_to :back
end
And this in my favorites view:
<% if user_signed_in? %>
<h1>Mijn favoriete recepten</h1>
<p>Bekijk hier jouw <b> nummer </b> favoriete recepten die jij lekker vindt.</p>
<div id="pins" class="transitions-enabled">
<% #pins.each do |pin| %>
<div class="box panel panel-default">
<div class="panel-body">
<b>Recept: </b>
<p><%= link_to pin.description, pin_path(pin) %></p>
<b>Toegevoegd door: </b>
<p><%= link_to image_tag(pin.user.image_file_name), pin %></p>
<div class="actions">
<%= link_to like_pin_path(pin), method: :put, remote: true, class: "btn btn-default" do %>
<span class="glyphicon glyphicon-heart"></span>
Vind ik lekker!!!!
<% end %>
<%= link_to unlike_pin_path(pin), method: :put, remote: true, class: "btn btn-default" do %>
<span class="glyphicon glyphicon-remove"></span>
Niet mijn smaak
<% end %>
</div>
</div>
</div>
<% end %>
</div>
<% else %>
<%= render 'pages/login' %>
<% end %>
Thanks in advance for your help!!
I have done something similar:
<% if user_signed_in? %>
<% #pins.each do |pin| %>
<% if current_user.voted_for? pin %>
That should be at the top of the file
I have changed the pins controller in the following and now it is working:
def my_favorites
#pins = current_user.find_liked_items
end
Thanks for your help!

Resources