【问题标题】:creating a user area on a rails site在 Rails 站点上创建用户区域
【发布时间】:2014-11-09 22:53:53
【问题描述】:

我想知道是否有人可以给我一些指导,让我知道接下来要采取的最佳步骤,因为我还在学习 rails。基本上,我有一个网站,允许用户注册、登录、注销和编辑他们的个人资料。我这样做是为了进行身份验证。我有一个网站管理区域,供网站所有者而不是公众使用。我选择这样做是因为它是一个电子商务网站,我希望用户部分更加定制。我现在想创建以下页面。

用户查看其订单历史记录的状态页面, 兑换优惠券的奖励页面, 供用户查看他们当前注册了哪些订阅的页面。

我在下面包含了我的用户代码,但如果有人可以帮助我解决这个问题,我将不胜感激,因为我无法完全理解下一步的最佳选择!

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


  has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100#" }, :default_url => "/images/:style/missing.png"
  validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/


  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  validates :name, presence: true

  has_many :listings, dependent: :destroy
  # # a listings existence depends on the existence of the user that created it
  # has_many :sales, class_name: "Order", foreign_key: "seller_id"
  has_many :purchases, class_name: "Order", foreign_key: "buyer_id"

【问题讨论】:

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


    【解决方案1】:

    Emma - 欢迎来到 Rails!首先,StackOverflow 真的是针对特定的编程问题。我知道你需要知道要走哪条路,所以这就是我要做的:


    设计

    既然您使用的是 Devise(伟大的宝石),让我为您提供一些有关其工作原理的信息。

    Devise 是一个authentication 系统 - 这意味着它处理您的应用程序的注册、登录和会话维护。具体来说,在使用 Devise 时,它​​只会用作管理对您的应用的访问的一种方式。 IE不用担心Devise

    你会找到great resource on Devise / Authentication here


    功能

    根据您的问题,您的系统的功能 应该包含在管理区域中。创建它实际上相对简单(所有繁重的工作都由 Devise 完成):

    #config/routes.rb
    namespace :admin do
       root: "application#dash"
       resources :orders
       resources :products
    end
    
    root: "products#index"
    resources :products, only: [:index, :show]
    

    这将使您能够使用以下代码:

    #app/controllers/products_controller.rb
    class ProductsController < ApplicationController
       #No authentication required
       def index
          @products = Product.all
       end
    
       def show
          @product = Product.find params[:id]
       end
    end
    

    注意到上面的控制器没有任何身份验证?它只是用来让您能够向用户展示产品/产品。这是“前端”,可用于从“产品”到“页面”等所有内容

    您正在寻找的功能位于“后端”——“管理”区域。我们通过使用:admin namespace 来实现这一点,它基本上为我们提供了一个以管理员为中心的控制器所在的文件夹:

    #app/controllers/admin/application_controller.rb
    class Admin::ApplicationController < ActionController::Base
       before_action :authenticate_user!
    
       def dash
          #stuff here
       end
    end
    
    #app/controllers/admin/products_controller.rb
    class Admin::ProductsController < Admin::ApplicationController
        ....
    end
    

    这样做的美妙之处在于admin 命名空间表面上是“自包含的”(admin/application_controller 管理身份验证,子控制器从它继承)。

    抛开细节不谈,这应该会给你一个稳定的平台来继续前进。

    --

    资源

    这里有一些很好的资源:

    【讨论】:

    • 谢谢,不幸的是我已经完成了上面的所有事情,这就是为什么我对下一步要去哪里有点卡住了。经过大量阅读后,作为最后的手段在这里提问。
    • 好吧,请详细说明您收到的错误等 - 我无法读懂您的想法 - 有什么问题?你发布了User 模特,那你想让我做什么?
    • 非常感谢您的帮助,但我没有意识到这不是我可以在这里问的问题,因为它不是那么具体。我需要创建列出的页面,但是已经完成了您建议的所有操作,我只是非常坚持如何前进。
    • 你的意思是你需要在你的视图中创建页面吗?
    猜你喜欢
    • 1970-01-01
    • 2018-12-02
    • 2016-08-18
    • 1970-01-01
    • 2012-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-05
    相关资源
    最近更新 更多