I know this is a really simple question, but I've gone completely blank! I'm reading through the Rails guides and looking at the getting started section. The following code is displaying all comments that belong to the current Post:
<h2>Comments</h2>
<% #post.comments.each do |comment| %>
<p>
<b>Commenter:</b>
<%= comment.commenter %>
</p>
<p>
<b>Comment:</b>
<%= comment.body %>
</p>
<% end %>
What is the simplest way of linking to each individual comment? For reference the page I am looking at is http://guides.rubyonrails.org/getting_started.html
The models are as follows:
class Post < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :post
end
Rails will try to create route helpers to assist you in this. You can get a full list by running rake routes, but there are good odds that the one you'll be looking for is named comment_path:
<% #post.comments.each do |comment| %>
<%= link_to 'Click to view comment', comment_path(comment) %>
<% end %>
For reference, check out the Rails Routing Guide.
You should take a look to your routes, and see what's the path to an individual comment.
It should be something like : comment_path(comment) or post_comment(#post, comment)
You will link it with <%= link_to "View comment", comment_path(comment) %>
Related
I have a #story record with the following attributes:
author
author_title
year
source
source_link
I'm rendering it in the view so it comes out like this:
James Joyce (author), 1882, Wikipedia
I am hoping there is a less convoluted way to generate the DOM for the citation than this (which is imperfect, as I explain below):
<%= #story.author %><% if !#story.author_title.blank? %> (<%= #story.author_title %>)<% end %><% if !#story.year.blank? %>, <%= #story.year %><% end %><% if !#story.source_link.blank? %>, <%= link_to #story.source, #story.source_link, target: "_blank" %><% end %>
As none of the fields are mandatory, the if-field-not-nil-then-you-may-need-a-comma issue is what I suspect could be handled more elegantly. For example, if author is blank, then I don't want to display the author_title or the trailing comma.
You can try the below code
create a two helper method
def author_story(author)
[#story.author_title, #story.year].compact.join(',')
end
def author_link
link_to(#story.source_link, text: 'testing')
end
and in view
<% if #story.author.present? %>
<div>
<span>
<%= author_story(#story) %>
</span>
<span>
<%= author_link(#story) %>
</span>
</div>
<% end %>
See my comments, Decorators is the way to go. You'll learn to love it and never go back.
If you think it is too much overhead for only a one-time simple task... then create a model method for this:
class Story < ApplicationRecord
.... #other stuff
def author_with_title_and_year
"#{author} #{author_title}, #{year}".squish
end
end
And add the link manually behind it:
<p><%= #story.author_with_title_and_year %> <%= link_to source, source_link %></p>
I'm developing an online course site using Ruby on Rails and bootstrap. I have a model for Courses and a model for Lessons. In the lesson controller show view I am not only showing the lesson page with video, discussion and notes but also a list of all the lessons belonging to the course. This is done by:
<%= #lessons.each do |lesson| %>
<%= link_to [lesson.course, lesson] do %>
<%= lesson.title %>
<% end %>
<% end %>
The user can, from the individual lesson page, pick the next lesson to watch.
Question:
The URL generated is: localhost:3000/courses/1/lessons/2 (I'll change this with Friendly Id gem later). I would like to show in the list of all lessons on the individual lesson page which lesson the user is watching right now. So basically maybe have something say "You are currently watching : Lesson with id 2" and have it in a different background color with some custom html. How can I have different HTML and CSS for the currently watching lesson in the all lessons list?
Thanks a lot in advance!
/Jacob
A simple way would be to add a conditional, in the case you have the current lesson available in a local variable or method current_lesson. Then it can look like this:
<%= #lessons.each do |lesson| %>
<% if lesson.id == current_lesson.id %>
... other html ...
<% else %>
<%= link_to [lesson.course, lesson] do %>
<%= lesson.title %>
<% end %>
<% end %>
<% end %>
I get an error undefined method 'email' for nil:NilClass when I try to access a post with a comment from a user that has been deleted in the DB.
I wonder: how can I remove the comments that has been created by users who no longer exists "on the fly"?
I tried something like this
<%= div_for(comment) do %>
<% if comment.user.email.nil?%>
<% comment.destroy %>
<%else%>
<p><%= comment.body %></p>
<p class="comment-submitted-by"><%= time_ago_in_words(comment.created_at) %> ago by
<%= comment.user.email %></p>
<%end%>
but I still get the error.
You're still getting an error because you've referenced comment.user.email, and user is nil. You need to check comment.user.nil?, or you're also at risk for deleting a comment just because a user's email is missing (though maybe you disallow that):
<% if comment.user.nil? %>
<% comment.destroy %>
Cleaning up on the fly is going to be finicky and cumbersome. What it looks like you want is dependent: :destroy on your User#comments association.
class User
has_many :comments, dependent: :destroy
end
Then when your User is removed, all of their Comments will be too, and you don't have to worry about it at display time.
Since there are existing orphaned comments, you can clean them out with a simple SQL statement:
DELETE FROM comments WHERE user_id NOT IN (
SELECT id FROM users
)
Or your Rails console:
Comments.find_each { |c| c.destroy if c.user.nil? }
Removing some comments from a thread might make it hard to understand the remaining comments, because of missing context.
Therefore I would like to suggest another approach: Just display deleted user instead of the user's email when the user was deleted.
A simple implementation might look like this:
<%= div_for(comment) do %>
<p><%= comment.body %></p>
<p class="comment-submitted-by">
<%= time_ago_in_words(comment.created_at) %> ago by
<%= comment.user ? comment.user.email : 'deleted user' %>
</p>
<%end%>
I have three models: posts, comments and questions. I have a link from my posts show page that's supposed to go to the questions show view. Instead it goes to the questions index view.
Here are the rake routes:
comment_question GET /comments/:comment_id/questions/:id(.:format) questions#show
comment_questions GET /comments/:comment_id/questions(.:format) questions#index
and here is the posts view:
<%= div_for(comment) do %>
<% comment.questions.select(:title).order('created_at desc').limit(3).each do |question| %>
<%= link_to (question.title), comment_question_path(comment, #question) %>
<% end %>
<% end %>
Thanks for the help.
Try using question instead of #question in your each block:
<%= link_to question.title, comment_question_path(comment, question) %>
If the helpers give you trouble you can always override them with manual implementation
<%= #question.title %>
I have the following rails 3 nested models:
resources :books do
resources :authors
end
I now have a view here: /books/131/authors/
And I want to each record to link to something like: /books/131/authors/3333
<% #authors.each do |author| %>
<%= link_to 'author.name', book_author_path(#book, #author) %>
<% end %>
but that error's with: No route matches {:action=>"destroy", :controller=>"authors"}
I also tried:
<%= link_to 'author.name', [#book, author] %>
Problem is the code keeps linking to /authors/3333, not /books/131/authors/3333
Ideas? thanks!
book needs to be defined in the author controller for def index
<%= link_to "title", book_author_path(#book, author) %>