【问题标题】:how do i redirect to home page(root )when user signed out?用户退出时如何重定向到主页(root)?
【发布时间】:2019-05-29 13:33:34
【问题描述】:

我正在尝试在我的简单 Rails 应用程序上进行设计;

1.我希望当用户退出时它会重定向到主页(root)。!

2.当用户创建自己的产品时,进入products.index会显示用户创建的所有产品,当以特定用户身份登录时,该人将看到自己创建的产品用户仪表板。(此时添加的产品将自动添加到产品索引页面)

非常感谢!

<header class="navbar">



  <nav class="menu">
    <ul>
      <li>
        <a href="#"> Products <i class="fa fa-sort-desc" aria-hidden="true"></i></a>
          <ul>
            <li>
              <%= link_to "Create a product",new_product_path %>
             </li>
            </li>
            </li>
            <li>
              <%= link_to "View all the products", products_path%></i>
            </li>
          </ul>
      </li>
      <li>
        <a href="#">Profile <i class="fa fa-sort-desc" aria-hidden="true"></i></a>
        <ul>
          <%= link_to "My dashboard", profile_path %>
          <%= link_to "Log out", destroy_user_session_path, method: :delete %>
        </ul>
      </li>
      <li><a href="#">Contact </a></li>
    </ul>
  </nav>


</header>
<h2>Log in</h2>


<%= simple_form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
  <div class='signup_create_product'>
    <%= f.input :email,
                required: false,
                autofocus: true,
                input_html: { autocomplete: "email" } %>
    <%= f.input :password,
                required: false,
                input_html: { autocomplete: "current-password" } %>
    <%= f.input :remember_me, as: :boolean if devise_mapping.rememberable? %>
  </div>

  <div class="form-actions">
    <%= f.button :submit, "Log in" %>
  </div>
<% end %>

<%= render "devise/shared/links" %>


class ProductsController < ApplicationController

  def index
    @products = Product.all
  end

  def show
    @product = Product.find(params[:id])
  end

  def new
    @product = Product.new
  end


  def create

     @product = Product.new(product_params)
     @product.user = current_user

    if @product.save
      redirect_to products_path
    else
      render :new
    end
  end

  def edit
    @product = Product.find(params[:id])
  end



  def destroy
    @product = Product.find(params[:id])
    @product.destroy
    redirect_to products_path
  end

  private

  def product_params
    params.require(:product).permit(:product_name, :description)
  end
end

用户展示页面

 <h1><%= @user.name %></h1>

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3


    【解决方案1】:

    从官方设计指南到退出后重定向:

    https://github.com/plataformatec/devise/wiki/How-To:-Change-the-redirect-path-after-destroying-a-session-i.e.-signing-out

    简而言之,将其添加到您的应用程序控制器中:

    class ApplicationController < ActionController::Base
      ...
      ...
      private
    
      # Overwriting the sign_out redirect path method
      def after_sign_out_path_for(resource_or_scope)
        root_path
      end
    end
    

    【讨论】:

      【解决方案2】:

      关于问题1:

      检查@Mark 答案

      关于问题2:

      假设你有这种关系:

      # app/models/user.rb
      
      class User < ApplicationRecord
        has_many :products
      end
      
      # app/models/product.rb
      
      class Product < ApplicationRecord
        belongs_to :user
      end
      

      user#show实现中添加

      # app/controller/users_controller.rb
      
      def show
        ...
        @products = current_user.products
        ...
      end
      

      现在在users/show 视图中,您可以循环@products 以获取用户的产品。

      <h1>User products</h1>
      
      <table>
        <tr>
          <th>Name</th>
          <th>Description</th>
          <th>Condition</th>
        </tr>
      
        <% @products.each do |product| %>
          <tr>
            <td><%= product.name %></td>
            <td><%= product.description %></td>
            <td><%= product.condition %></td>
          </tr>
        <% end %>
      </table>
      

      【讨论】:

      • 没问题!非常感谢!在 product.index 页面中,我想显示用户创建的所有产品,并在特定用户仪表板(用户/显示)中显示;我想显示特定用户创建的要显示的产品他/她自己的仪表板。
      • @developing 我编辑了我的答案,如果这就是你要找的,请告诉我。
      • 嗨@alexalth 谢谢!在用户/节目上是这样的吗? ```

        ``` 但它没有出现在仪表板上,我可以为您提供任何其他文件吗?谢谢!
      • @developing 在循环内删除@,因此将每个产品称为product 而不是@product。仅使用 @ 引用集合 @products,例如@products.each do |product|
      • 嗨@alexalth 非常感谢你!视图/显示有很大帮助,但是在我在用户/显示方法中执行此操作后,它现在可以工作了! @products = current_user.products
      猜你喜欢
      • 1970-01-01
      • 2011-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-21
      • 2011-05-13
      • 1970-01-01
      相关资源
      最近更新 更多