mercredi 29 mai 2019

how do i redirect to home page(root )when user signed out?

I am trying to get devise working on my simple rails app where;

1.I'd like when user_signed out it will redirect to home page(root).for the moment it's showing the page as many thanks!

and 2.when the user create his/her own products, the products will "only" display in the person's dashboard.(at the moment the added products will automatically added to the products index page)

<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

user show page

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

Aucun commentaire:

Enregistrer un commentaire