"Previous Post" and "Next Post" Link in Show View (Nested Resources)

Previous post and Next post link in Show View (Nested Resources)

Simply use

<%= link_to 'next', user_article_path(@user, @article.next) %>

And rest by analogy.

Rails: Next post and Previous post links in my show view, how to?

If each title is unique and you need alphabetical, try this in your Post model.

def previous_post
self.class.first(:conditions => ["title < ?", title], :order => "title desc")
end

def next_post
self.class.first(:conditions => ["title > ?", title], :order => "title asc")
end

You can then link to those in the view.

<%= link_to("Previous Post", @post.previous_post) if @post.previous_post %>
<%= link_to("Next Post", @post.next_post) if @post.next_post %>

Untested, but it should get you close. You can change title to any unique attribute (created_at, id, etc.) if you need a different sort order.

Creating link_to for sequential nested resources

Because of Rails magic, you can pass the resource directly to the route helper, and it will use the correct id for that resource. For example:

<% @foo.bars.each do |bar| %>
<%= link_to bar.name, foo_bar_path(@foo, bar) %>
<% end %>

The above assumes that your route file looks something like:

resources :foos do
resources :bars
end

I highly recommend Rails Routing from the Outside In; it's been a very helpful resource for me!

To set the order of the child resource, you could use a scope, like this:

class Bar < ActiveRecord::Base
scope :ordered, -> { order(id: :asc) }
end

And then in your view, call foo.bars.ordered.each do |bar| etc.... Your nested resource will be returned from lowest to highest ID, skipping over any that have been deleted.

I hope this helps with what you were asking.

EDIT

I misread the question. To dynamically generate the "next" id, you could create method, next, on your child class. This answer seems to be something like what you want. Then in your view, you can just call:

<%= link_to "Next", bar_path(current_bar.next) %>

Rails: Previous/Next in record when dealing with nested routes

You should be able to use user_photo_path to get a nested link:

<%= link_to "Previous", user_photo_path(@photo.user, @photo.previous_img) if @photo.previous_img %>

How to use Link_to with nested resources

After trying all of the answers it didnt completely work, but i found a way to resolve it.
In a first moment, I used

post_comments_url(@post,comment)

where comment is the item inside a @post.each.

It generates a "strange" route, using . instead / like "post/34/comments.2", i fixed it using the singular form:

post_comment_url(@post,comment)

Thanks for helping!

Rails Routes adding '.' to nested resource show page?

Use singular line, not plural lines:

manufacturer_line_path(line.manufacturer, line)

UPDATE:

Your destroy link looks fishy as well - it should be the same url as for the show action.

Why is my nested resource form trying to use a non-existent path method when I've already set the URL?

The form_with helper build the nested url from the array you passed. But in case of new record, @data_set.author returns nil and the form tries to reach data_sets_path which does not exist.

If your form is just for the "new" action you can write:

<%= form_with model: @data_set, url: [:user, @data_set] do |form| %>

If you share the form between "new" and "edit", you can write a condition like that:

<%= form_with model: @data_set, url: [(@data_set.new_record? ? :user : @data_set.author), @data_set] do |form| %>


Related Topics



Leave a reply



Submit