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 %>
Related
I am building a basic bare bones social media app right now.
I have a user class and a status class.
For each status, there is a "creater" (a user object) and a "subject" (a user object that the status is about). I was able to create tags by using the acts_as_taggable_on gem. What ends up happening is when a user goes to create a post, he/she can select another user from a dropdown menu. The chosen user's id attribute is then stored.
Now I am trying to link to the chosen User's profile. This is my code for show statuses on a profile page.
<% if #statuses %>
<% #statuses.each do |status| %>
<div class="well">
<%= status.content %>
<br></br>
#link to user who's associated with the tagId
<%= link_to User.find(status.tag_list).profile_name, user_profile_path(User.find(status.tag_list).profile_name) %>
<hr />
<%= link_to time_ago_in_words(status.created_at), status_path(status) %> ago
</div>
<% end %>
<% end%>
this is the line where the above code breaks
<%= link_to User.find(status.tag_list).profile_name, user_profile_path(User.find(status.tag_list).profile_name) %>
Can anyone help me out with this?
Not surprised this line is failing:
<%= link_to User.find(status.tag_list).profile_name, user_profile_path(User.find(status.tag_list).profile_name) %>
A couple points:
It's a little cleaner to separate it onto multiple lines
I suspect your problem is because you're passing a profile_name to user_profile_path instead of an id, though I can't be certain without seeing your routes.
Try the following:
<% profile_user = User.find(status.tag_list) %>
<%= link_to profile_user.profile_name, user_profile_path(profile_user.id) %>
I have a loop of questions where each question belongs to a post and the post title is displayed above the question. If 2 ( or 3 or 4 etc) questions in a row belong to the same post I only want to display the post name once. My idea was to use an index to check if the prior questions post == the current questions post. The problem is I'm not sure how that would work.
Here is what I tried:
<% #questions.each_with_index do |question, i| %>
<% unless (i-1).comment.post == question.comment.post %>
<%= question.comment.post.title %>
<% end %>
<% end %>
This gives me an undefined method comment error since I can't call '(i-1).comment' but can I do something like that or is there a better way to do this?
One way to do this would be to use group_by to group the questions by post:
<% #questions.group_by {|q| q.comment.post}.each do |post, questions| %>
<%= post.title %>
<% for question in questions %>
<%= question.title %>
<% end %>
<% end %>
If you need to maintain the order of the questions, you could use each_cons.
<%= #questions.first.comment.post.title %>
<% #questions.each_cons(2) do |previous_question, question| %>
<% unless previous_question.comment.post == question.comment.post %>
<%= question.comment.post.title %>
<% end %>
<% end %>
Couple of minor notes:
This is a troubling Law of Demeter violation (having to traverse from the question to the comment to the post and eventually the title).
This sort of complicated data manipulation is much better in a Ruby object than in a template. Ideally, templates are dead simple.
I think I am deeply misunderstanding how to write instances.
Miniatures have_many Manufacturers through the Productions table.
On the miniatures show page I am trying to list all the manufacturers for the current miniature and have them link the Manufacturer show page. Like so:
<% #miniature.manufacturers.each do |manufacturer| %>
<%= link_to #miniature.manufacturer.name, manufacturer_path %>
<% end %>
Needless to say it does not work. It gives "undefined method `manufacturer'".
I have tried A LOT of different combinations to no avail. The following version puts all the manufacturers, rolled into one link, once for each manufacturer a miniature has, and links to /manufacturers. A big mess.
<% #miniature.manufacturers.each do |manufacturer| %>
<%= link_to #miniature.manufacturers.map(&:name).join(', '), manufacturer_path %>
<% end %>
I have been working on other things and hoping I would get the hang of this but I'm pretty sure it's something pretty fundamental about how I set up the instance.
If it's more likely something I need to add to the controller then I can add my controller code here. Any help much appreciated.
Does this work:
<% #miniature.manufacturers.each do |manufacturer| %>
<%= link_to manufacturer.name, manufacturer_path(manufacturer) %>
<% end %>
I'm new to rails and am working on extending the functionality of my basic blog app. What I would like to do is create date-based navigation links. For example, I would like to have a list of links with the names of the months (as the links) and when you click on the month it shows you all the articles published in that month.
I'm struggling with how to best accomplish this.
Should I create a new Model / View / Controller for something like an ArticleArchive? Or is the solution more simple based on my needs?
I've searched the other posts in the community and none seemed to answer this. Any help with how to structure this and possibly implement is appreciated. Thanks!
Here's an example on approaching this, although in this I wanted to sort it by day. This is for your controller action:
def index
#article_days = Article.all.group_by{ |r| r.published_at }
end
To modify this to months, you'd want to do something like r.published_at.beginning_of_month in the example above and essentially group_by the name of the month.
In the view template:
<% #article_days.sort.each do |pub, articles| %>
<h3><%= pub.strftime('%e %A, %B %Y') %></h3>
<% for article in articles %>
<%= article.title %><br/>
<%= article.summary %>
<% end %>
<% end %>
There's a screencast on this as well.
UPDATE
OK - so you only want the names of the month appearing. Keep the instance variable we setup in your index action along with your other code (you probably have setup #articles = Article.all). Then where you want the links listed do:
<% #article_months.sort.each do |pub, articles| %>
<h3><%= pub.strftime('%B') %></h3>
<% for article in articles %>
<%= link_to "#{article.title}", article_path(article) %>
<% end %>
<% end %>
I'm very new to programming and have followed this screencast at emersonlackey.com on adding multiple paperclip uploads for a record. It all works great but I can't figure out how to display the uploaded images in the records show page.
They display fine on the edit page using:
<%= f.fields_for :venuephotos do |photo| %>
<% unless photo.object.new_record? %>
<p>
<%= link_to image_tag(photo.object.venuephoto.url(:thumb)), photo.object.venuephoto.url(:original) %>
<%= photo.check_box :_destroy %>
</p>
<% end %>
<% end %>
I've used paperclip in other models but they are the standard each record only has one upload so the images display fine with <%= #model.photo.url %> type call but I cant figure out this other way.
Any help is much apprectiated!
You want to do something like this:
<% for asset in #post.assets %>
<%= link_to image_tag(asset.asset.url(:thumb)), asset.asset.url(:original) %>
<% end %>
as per the code that goes along with the screencast.
https://github.com/Emerson/Multiple-File-Uploads-with-Paperclip-and-Rails-3/blob/master/app/views/posts/show.html.erb