I am trying to display a list of accounts, i am getting the above error in my link_to method. Below is my link_to method.
<div class="list-group">
<% #accounts.each do |account| %>
<%= link_to account.name, account_path( account.accountid ),
class: "list-group-item active" do %>
<span class="glyphicon glyphicon-chevron-right"></span>
<span class="badge">44</span>
<% end %>
<% end %>
</div>
If you're using the block form of link_to you can't have text content (the block is your text content). If you look at rails helpers. The syntax for block form looks like:
link_to(url, html_options = {}) do
# name
end
So you need to do something like:
<div class="list-group">
<% #accounts.each do |account| %>
<%= link_to account_path( account.accountid ), class: "list-group-item active" do %>
<%= account.name %>
<span class="glyphicon glyphicon-chevron-right"></span>
<span class="badge">44</span>
<% end %>
<% end %>
</div>
Related
Below is code extract from my file app/views/discussions/show.html.erb where line 16 raised this error:
undefined method `markdown' for #<#<Class:0x000000000c94e0d8>:0x000000000c94c6e8>
s<div class="columns">
<div class="column is-8">
<h1 class="title is-2 has-text-grey discussion-title"><%= #discussion.title %></h1>
<h3 class="subtitle is-5 has-text-grey-lighter">by <%= #discussion.user.username %> in <%= link_to #discussion.channel.channel, #discussion.channel %></h3>
<div class="level">
<div class="level-left"></div>
<div class="level-right">
<% if discussion_url(#discussion) %>
<div class="buttons">
<%= link_to 'Edit Discussion', edit_discussion_path(#discussion), class:'button'%>
<%= link_to 'Delete', discussion_path(#discussion), method: :delete, data: { confirm: "Delete discussion?" }, class:'button' %>
</div>
<% end %>
</div>
</div>
<div class="content"><%= markdown (#discussion.content) %></div>
<!-- ^^^^^^^^ -->
<h2 class="subtitle is-5 has-text-grey"><%= #discussion.replies.count %> Replies</h2>
<div id="discussion-replies">
<%= render #discussion.replies %>
</div>
<hr/>
<h3 class="subtitle is-3 has-text-grey">Leave a reply</h3>
<% if user_signed_in? %>
<%= render 'replies/form' %>
<% else %>
<p>To reply you need to <%= link_to 'login', new_user_session_path %>. Don't have an account?
<%= link_to 'Sign up', new_user_registration_path %> for one.</p>
<% end %>
</div>
<%= render 'sidebar' %>
</div>
cant view reply or comment section
Rails doesn't include a markdown method. You'll have to use a gem, write something yourself or a combination of the two. You could for example use the redcarpet gem, or one of the other markup processor gems.
Then write your own helper using this using this gem.
# app/helpers/markdown_helper.rb
module MarkdownHelper
MARKDOWN = Redcarpet::Markdown.new(Redcarpet::Render::HTML)
def markdown(markdown_string)
MARKDOWN.render(markdown_string).html_safe
end
end
For usage and possible render configurations checkout out the redcarpet documentation.
With this helper present you can simply do the following in the view:
<%= markdown(#discussion.content) %>
How to loop through post that are on promoted. I have a method in my post model that I can access. How would I call it in this instance. For example here are my methods my post model.
class Post < ApplicationRecord
def promoted?
subscriptions.present?
end
def self.promoted_posts
Post.joins(:subscriptions).where(:subscriptions => {:is_active => true})
end
def self.not_promoted_posts
Post.left_outer_joins(:subscriptions).where(:subscriptions => {:post_id => nil})
end
def self.ordered_posts
Post.promoted_posts + Post.not_promoted_posts
end
end
Here is my view. I would like to loop through all the posts that are promoted which is the method "def promoted?" in my model
<% #posts.take(6).each do |post| %>
<div class="col-xs-6 col-sm-6 col-md-6 col-lg-4">
<div class="featured-box">
<figure>
<%= link_to image_tag(post.images.first, class: 'img-fluid'), post %>
</figure>
<div class="feature-content">
<div class="product">
<p><%= post.category.name %> / <%= post.subcategory.name %></p>
</div>
<h4><%= link_to post.title, post %></h4>
<ul class="address">
<li>
Ad#: <%= post.ad_number %>
</li>
<li>
posted <%= time_ago_in_words(post.created_at) %> ago
</li>
<li>
<%= post.state%> , <%= post.city.downcase %>
</li>
</ul>
<div class="listing-bottom">
<h3 class="price float-left">$<%= post.price%></h3>
<%= link_to 'Feature Ad', post, class: 'btn-verified float-right', style: 'color: red;' %>
</div>
</div>
</div>
</div>
<% end %>
If you are desperately in need of using the promoted, you can try
Post.all.find_all(&:promoted?)
which is short notation of
Post.all.find_all do |post|
post.promoted?
end
which is kinda n+1 query and opting it while you can just use
Post.joins :subscriptions
is a fail.
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
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!
Basically my controller is just grabbing all members: #members = Member.all and Im looping through them while checking to see if they have a profile picture uploaded and if not then the default should be loaded:
<% #members.each do |member| %>
<% unless member.image.nil? %>
<li style="float:left; width:100px;">
<%= image_tag(member.image.url(:tiny)) %>
<%= link_to member.email, member_path(member) %>
</li>
<% else %>
<li style="float:left; width:100px;">
<%= image_tag("default_member_small.jpg") %>
<%= link_to member.email, member_path(member) %>
</li>
<% end %>
<% end %>
It seems to think every member has a profile image, and the image tag is calling "images/tiny/missing.png" for the missing images.
What gives?
I am guessing you are using paperclip, if you are, you should not use nil?, you should use present?:
<% #members.each do |member| %>
<% if member.image.present? %>
<li style="float:left; width:100px;">
<%= image_tag(member.image.url(:tiny)) %>
<%= link_to member.email, member_path(member) %>
</li>
<% else %>
<li style="float:left; width:100px;">
<%= image_tag("default_member_small.jpg") %>
<%= link_to member.email, member_path(member) %>
</li>
<% end %>
<% end %>
And instead of having an if you should just have this image named as paperclip expects it, there should not have any ifs in your code for this kind of handling.