用户注销后如何重定向到主页(根)?

时间:2019-05-29 13:33:35

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

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

1。我希望当user_signout退出时,它将重定向到主页(根)。!

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>

2 个答案:

答案 0 :(得分:2)

从正式的设计指南中退出后重定向:

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

答案 1 :(得分: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>