“上一篇"和“下一篇文章"显示视图中的链接(嵌套资源) [英] "Previous post" and "Next post" link in Show View (Nested Resources)

查看:41
本文介绍了“上一篇"和“下一篇文章"显示视图中的链接(嵌套资源)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我想在文章显示视图的底部添加上一篇文章"和下一篇文章"链接.

这是我目前所拥有的,但出现此错误:

This is what I have so far but I get this error:

undefined method `article_path' for #<#<Class:0x007fd7c581af48>:0x007fd7cb8e5968>

我知道路径必须是这样的(但我很难实现它)

I know the path must look like this (but i am having a hard time implementing it)

myapp/users/1/article/1 

铁路新手请帮助...

New to Rail Please Help ...

路线

resources users do
  resources articles
end

模型

class User < ActiveRecord::Base
 attr_accessible :name, :photo

 has_many :articles

end

class Article < ActiveRecord::Base
  attr_accessible :name

  belongs_to :user

  def next
    user.articles.where("id > ?", id).order("id ASC").first
  end

  def prev
    user.articles.where("id < ?", id).order("id DESC").first
  end

end

视图

文章显示页面 appname/users/1/articles/1

Articles Show Page appname/users/1/articles/1

<%= link_to @article.name %>

<%= link_to "next", @article.next %>
<%= link_to "previous", @article.prev %>

控制器

class ArticlesController < ApplicationController

  before_filter :get_publisher

  def get_user
    @user = User.find(params[:user_id])
  end

  def show
    @article = @user.articles.find(params[:id])
  end

  def index
    @articles = @user.articles
  end

end

推荐答案

简单使用

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

以此类推.

这篇关于“上一篇"和“下一篇文章"显示视图中的链接(嵌套资源)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆