仅在视图/方法中创建登录

时间:2014-07-23 07:24:51

标签: ruby-on-rails ruby ruby-on-rails-4 devise

我使用设计创建了一个用户模型。我想知道如何创建仅在我登录后才能工作的视图和方法?

这是我的模特:

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

end

控制器:

class UserController < ApplicationController

  def create
    User.create(user_params)
  end

  private

    def user_params
      # required input for params
      # permit - returns a version of the params hash with ony the permitted attributes
      params.require(:user).permit(:name, :email, :password, :password_confirmation)
    end

end

1 个答案:

答案 0 :(得分:1)

您将保护控制器及其操作。

before_action :authenticate_user!

user_signed_in?

current_user

user_session

了解更多!

https://github.com/plataformatec/devise