I'm trying to create a link that will direct people to the users profile page by clicking on their name when they look at their pics. The line of code is below however, I am not sure how to turn this into a link that will direct people to the users profile. Right now the users post displays their name in association with their post. Hopefully this makes sense Thanks in advance.
<p><strong><%= #post.user.name if #post.user %></strong></p>
The route to the users profile page is users_path.
Post/Show.html
<div class="row">
<div class="col-md-offset-4 col-med-8">
<div class="panel panel-default">
<div class="panel-heading center">
<% if #post.image.url %>
<%= image_tag #post.image.url(:medium) %>
<% elsif #post.video.url %>
<%= video_tag #post.video.url(:medium), controls: true, type: "video/mp4" %>
<% end %>
</div>
<div class="panel-body">
<p><%= #post.description %></p>
<p><strong><%= #post.user.name if #post.user %></strong></p>
<% if #post.user == current_user %>
<%= link_to edit_post_path(#post) do %>
<span class="glyphicon glyphicon-edit"></span>
Edit
<% end %>
<% end %>
<%= link_to 'Back', posts_path %>
</div>
</div>
Considering user_path is path to user's profile
<p>
<strong>
<%= link_to(user.name, user_path(user)) if user = #post.user %>
</strong>
</p>
I did this like this... <%= link_to (#post.user.name), user_path(#post.user) %>
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) %>
In my rails app, I have an index of an object I'm calling Lessons. If a user marks the lesson as being completed, it gets recorded in a join table called Completions and the completed_step boolean gets set to true.
In the show action for the lesson, I'm able to insert a conditional statement to modify the button for "Complete Lesson."
That button logic works fine like this:
<% if current_user.completed_steps.include? #lesson %>
<%= button_to "This Lesson is Completed", #lesson.next, class: "btn btn-success btn-lg", :method => :get %>
<% else %>
<%= button_to "Mark this Lesson as Complete", complete_lesson_path, class: "btn btn-warning btn-lg" %>
<% end %>
My question is how do I incorporate this type of logic check on the index view?
I'd like the links to each Lesson to appear in a Bootstrap Panel and I want to change the color of the panel depending upon whether or not the user completed_step for each Lesson.
I tried wrapping the panel in the same statement and it doesn't work, it stays red even though the user has completed these lessons.
Here's what I attempted:
<% #lessons.each do |lesson| %>
<% if current_user.completed_steps.include? #lesson %>
<div class="panel panel-success">
<% else %>
<div class="panel panel-danger">
<% end %>
<div class="panel-heading">
<h3 class="panel-title">
<%= link_to(lesson) do %>
<strong><%= lesson.title %></strong>
<% end %>
</h3>
</div>
<div class="panel-body">
<td><%= lesson.summary %></td>
</div>
</div>
What am I doing wrong or why isn't this working?
Please change your script
<% #lessons.each do |lesson| %>
<% if current_user.completed_steps.include? lesson %>
<div class="panel panel-success">
<% else %>
<div class="panel panel-danger">
<% end %>
<div class="panel-heading">
<h3 class="panel-title">
<%= link_to(lesson) do %>
<strong><%= lesson.title %></strong>
<% end %>
</h3>
</div>
<div class="panel-body">
<td><%= lesson.summary %></td>
</div>
</div>
You need to pass same object as you given in in you for loop .
<% #lessons.each do |lesson| %>
<% if current_user.completed_steps.include? lesson %>
<div class="panel panel-success">
<% else %>
#Pramod Gupta is right. Also, look into using Presenters for this type of logic that is used for decorating the views.
#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!
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!
I have created commenting system to provide functions like creating comments and replying them.
I used http://www.sitepoint.com/nested-comments-rails/ guide. Worked just perfect. But in this example to reply to some comment it goes to other path, that is what I want to avoid.
Code so far:
Advertisement#show here I want to create reply to comment.
<%= comments_tree_for #comments %>
<h1>New comment</h1>
<%= render 'comments/form' %>
_comment.html.rb
<div class="well">
<h2><%= comment.title %></h2>
<p class="text-muted"><%= comment.root? ? "Started by" : "Replied by" %> <strong><%= comment.author %></strong> on
<%= l(comment.created_at, format: '%B, %d %Y %H:%M:%S') %></p>
<blockquote>
<p><%= comment.body %></p>
</blockquote>
<% from_reply_form ||= nil %>
<% unless from_reply_form %>
<% if comment.leaf? %>
<small class="text-muted">There are no replies yet - be the first one to reply!</small>
<% end %>
<p><%= link_to 'reply', new_comment_path(comment.id) %></p>
<% end %>
</div>
_form.html.erb
<%= form_for(#comment) do |f| %>
<% if #comment.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#comment.errors.count, "error") %> prohibited this comment from being saved:</h2>
<ul>
<% #comment.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.hidden_field :advertisement_id, :value => #advertisement.id%>
<%= f.hidden_field :user_id, :value => current_user.id%>
<%= f.hidden_field :parent_id %>
<div class="form-group">
<%= f.label :body %>
<%= f.text_area :body, class: 'form-control', required: true %>
</div>
<%= f.submit class: 'btn btn-primary' %>
<% end %>
Is there any trustworthy guide to help me ?
The link_to 'reply', new_comment_path(comment.id) line creates a hyperlink to new comment page and when clicked it takes you to next page to create the comment. Instead you can replace it with a div containing the form the toggles and submits new convo from the same page. Bootstrap comes handy here.
A Mock up of the idea: http://jsfiddle.net/bpya4fce/1/
You can build up using this idea. Hope this helps :)
P.S: Make sure that you fetch the variables required for the form in the action of view where you embed the form, i.e if you're embedding the form in show.html.erb, ensure the necessary variables are fetched in the show action/method of controller. In the earlier scenario, it'll be fetched in the new action of the CommentsController.
<div class="container">
<blockquote>
<h2>Posted Comment</h2>
Lorem Ipsum. You can reply to this below.
</blockquote>
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion"
href="#collapseOne">
Click to reply
</a>
</h4>
</div>
<div id="collapseOne" class="panel-collapse collapse">
<div class="panel-body">
### The comment form comes here ###<br/>
### render :partial => 'comment/form'
</div>
</div>
</div>
</div>
Not sure if this is what you wanted. Hope this example code gives you some idea.
Be sure to complete the code and test it :)
_comment.html.rb
<% from_reply_form ||= nil %>
<% unless from_reply_form %>
<% if comment.leaf? %>
<small class="text-muted">There are no replies yet - be the first one to reply!</small>
<% end %>
<!-- HERE adding a hidden DIV that contains a form. -->
<div class='hidden-reply-form-<%= comment.id%>'>
<%= render partial: 'comments/form', locals: {comment: Comment.new} %>
</div>
<p><%= link_to 'reply', new_comment_path(comment.id), class: 'reply', id: comment.id %></p>
<% end %>
</div>
<script>
// HERE use reply link to toggle the hidden div.
$(function(){
$(".reply").click(function(){
// toggle replying div.
// .....
});
})
</script>