代码之家  ›  专栏  ›  技术社区  ›  Akash

如何在表单中使用Spree的身份验证

  •  0
  • Akash  · 技术社区  · 9 年前

    我正在学习rails,并创建一个网络应用程序,其中也包含了电子商务 有一个表单,用户只有在登录后才能填写,为此我使用了Devise,然后我安装了Spree用于电子商务 Spree有自己的登录身份验证,但没有authenticate_user!在控制器中, 我移除了这个装置,很难找到如何在我的表单中使用Spree的身份验证

    这是 已更新 表单的控制器: 投诉_控制器.rb

    module Spree
    class ComplaintsController < Spree::StoreController
      before_action :require_login
    
      before_action :set_complaint, only: [:show, :edit, :update, :destroy]
    
      # GET /complaints
      # GET /complaints.json
    
    
    
     def require_login
          redirect_to spree_login_path unless current_spree_user
        end 
    
    
          def index
            @complaints = Complaint.all
          end
    
      # GET /complaints/1
      # GET /complaints/1.json
      def show
      end
    
      # GET /complaints/new
      def new
        @complaint = Complaint.new
      end
    
      # GET /complaints/1/edit
      def edit
      end
    
      # POST /complaints
      # POST /complaints.json
      def create
        @complaint = Complaint.new(complaint_params)
    
        respond_to do |format|
          if @complaint.save
            format.html { redirect_to @complaint, notice: 'Complaint was successfully created.' }
            format.json { render :show, status: :created, location: @complaint }
          else
            format.html { render :new }
            format.json { render json: @complaint.errors, status: :unprocessable_entity }
          end
        end
      end
    
      # PATCH/PUT /complaints/1
      # PATCH/PUT /complaints/1.json
      def update
        respond_to do |format|
          if @complaint.update(complaint_params)
            format.html { redirect_to @complaint, notice: 'Complaint was successfully updated.' }
            format.json { render :show, status: :ok, location: @complaint }
          else
            format.html { render :edit }
            format.json { render json: @complaint.errors, status: :unprocessable_entity }
          end
        end
      end
    
      # DELETE /complaints/1
      # DELETE /complaints/1.json
      def destroy
        @complaint.destroy
        respond_to do |format|
          format.html { redirect_to complaints_url, notice: 'Complaint was successfully destroyed.' }
          format.json { head :no_content }
        end
      end
    
      private
        # Use callbacks to share common setup or constraints between actions.
        def set_complaint
          @complaint = Complaint.find(params[:id])
        end
    
        # Never trust parameters from the scary internet, only allow the white list through.
        def complaint_params
          params.require(:complaint).permit(:id_society, :id_user, :heading, :text, :active, :action, :IsDelete, :flat_number)
        end
    end
    end
    <% end %>
    

    索引html.erb

      <% if spree_current_user %>
      <p id="notice"><%= notice %></p>
    
    <h1>Listing Complaints</h1>
    
    <table>
      <thead>
        <tr>
          <th>Id society</th>
          <th>Id user</th>
          <th>Heading</th>
          <th>Text</th>
          <th>Active</th>
          <th>Action</th>
          <th>Isdelete</th>
          <th>Flat number</th>
          <th colspan="3"></th>
        </tr>
      </thead>
    
      <tbody>
        <% @complaints.each do |complaint| %>
          <tr>
            <td><%= complaint.id_society %></td>
            <td><%= complaint.id_user %></td>
            <td><%= complaint.heading %></td>
            <td><%= complaint.text %></td>
            <td><%= complaint.active %></td>
            <td><%= complaint.action %></td>
            <td><%= complaint.IsDelete %></td>
            <td><%= complaint.flat_number %></td>
            <td><%= link_to 'Show', complaint %></td>
            <td><%= link_to 'Edit', edit_complaint_path(complaint) %></td>
            <td><%= link_to 'Destroy', complaint, method: :delete, data: { confirm: 'Are you sure?' } %></td>
          </tr>
        <% end %>
      </tbody>
    </table>
    
    <br>
    
    <%= link_to 'New Complaint', new_complaint_path %>
    
    <% else %>
    <h1> please login</h1>
    <% end %>
    

    这是有效的,因为它在View中验证用户的身份验证,有没有方法在控制器中检查它?比如,如果用户登录,它将被发送到操作或重定向到登录?

    非常感谢。

    1 回复  |  直到 9 年前
        1
  •  1
  •   dipak gupta    9 年前

    Spree通过扩展使用design身份验证:

    https://github.com/spree/spree_auth_devise  
    

    为了在控制器(您自己的控制器)级别验证您的操作,您需要定义自己的验证过滤器。所以你可以这样做:

    before_action :require_login
    
    def require_login
      redirect_to login_url unless current_spree_user
    end