BookmarksController #index中的NoMethodError

时间:2014-03-21 22:14:35

标签: ruby-on-rails ruby activerecord devise

安装设备并添加身份验证后&用户模型成功,每次我尝试注册我的网络应用程序时,我都可以进入登录页面并登录但是当我点击提交时,我在BookmarksController中获取NoMethodError #index undefined method`page'对于#

# GET /bookmarks
  def index
error -> @bookmarks = current_user.bookmarks.order('created_at desc').page(params[:page])
  end

  # GET /bookmarks/1

书签控制器

class BookmarksController < ApplicationController
  before_action :set_bookmark, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!

  # GET /bookmarks
  def index
    @bookmarks = current_user.bookmarks.order('created_at desc').page(params[:page])
  end

  # GET /bookmarks/1
  def show
  end

  # GET /bookmarks/new
  def new
    @bookmark = current_user.bookmarks.new
  end

  # GET /bookmarks/1/edit
  def edit
  end

  # POST /bookmarks
  def create
    @bookmark = current_user.bookmarks.new(bookmark_params)

    if @bookmark.save
      redirect_to @bookmark, notice: 'Bookmark was successfully created.'
    else
      render action: 'new'
    end
  end

  # PATCH/PUT /bookmarks/1
  def update
    if @bookmark.update(bookmark_params)
      redirect_to @bookmark, notice: 'Bookmark was successfully updated.'
    else
      render action: 'edit'
    end
  end

  # DELETE /bookmarks/1
  def destroy
    @bookmark.destroy
    redirect_to bookmarks_url, notice: 'Bookmark was successfully destroyed.'
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_bookmark
      unless @bookmark = current_user.bookmarks.where(id: params[:id]).first
        flash[:alert] = 'Bookmark not found.'
        redirect_to root_url
      end
    end

    # Only allow a trusted parameter "white list" through.
    def bookmark_params
      params.require(:bookmark).permit(:title, :url, :user_id)
    end
end

bookmark.rb
class Bookmark < ActiveRecord::Base

    belongs_to :user

end

user.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :bookmarks
end

1 个答案:

答案 0 :(得分:1)

page操作中移除index方法的来电:

  def index
    @bookmarks = current_user.bookmarks.order('created_at desc')
  end

它没有在问题的给定代码中的任何位置定义。 page方法通常与will_paginate gem一起使用,您没有使用它,因此请删除该调用。