12步让商品可以隐藏

Published on:

hide product

1.migration
$ rails g migration add_is_hidden_to_product is_hidden:boolean

db/migrate/xxxxxxxxx_add_is_hidden_to_job.rb
class AddIsHiddenToJob < ActiveRecord::Migration[5.0]
    def change
        add_column :jobs, :is_hidden, :boolean, default: false
    end
end

$ rake db:migrate

2.add is_hidden to new/edit

app/views/admin/products//new&edit.html.erb
<%= f.input :is_hidden %>

3.add is_hidden to product_params

app/controllers/admin/products_controller.rb
def product_params
params.require(:product).permit(:title, :description, :is_hidden)
  end

4.hide on index page and order by time

app/controllers/products_controller.rb
def index
    @products = product.where(:is_hidden => false).order("created_at DESC")
  end

5.hide info on backend

app/views/admin/products/index.html.erb
 <td>

        <% if product.is_hidden %>
         (Hidden)
      <% else %>
         (Public)
      <% end %>

      <%= link_to(product.title, admin_product_path(product)) %>
  </td>

6.reflect to helper

app/views/admin/products/index.html.erb
<td>
  <%= render_product_status(product) %>

  <%= link_to(product.title, admin_product_path(product)) %>
</td>
app/helpers/products_helper.rb
module productsHelper

  def render_product_status(product)
    if product.is_hidden
      "(Hidden)"
    else
      "(Public)"
    end
  end
end

7.use icon

app/views/admin/products/index.html.erb
 def render_product_status(product)
    if product.is_hidden
      content_tag(:span, "", :class => "fa fa-lock")
    else
      content_tag(:span, "", :class => "fa fa-globe")
    end
  end

8.hide info action

app/controllers/products_controller.rb
def show
    @product = product.find(params[:id])

    if @product.is_hidden
      flash[:warning] = "This product already archieved"
      redirect_to root_path
    end
  end

9.hide button

app/views/admin/products/index.html.erb
<td>
  <%= link_to("Edit", edit_admin_product_path(product)) %> 
  <%= link_to("Destroy", admin_product_path(product), :method => :delete, :data => { :confirm => "Are you sure?" }) %>

  
  <% if product.is_hidden %>

    <%= link_to("Publish", publish_admin_product_path(product), :class => "btn btn-xs btn-default") %>
  <% else %>
    <%= link_to("Hide", hide_admin_product_path(product), :class => "btn btn-xs btn-default") %>
  <% end %>
</td>

10.add to routes

config/routes.rb
namespace :admin do
    resources :products do
      member do
        post :publish
        post :hide
      end
    end
  end

11.admin action

app/controllers/admin/products_controller.rb
def publish
    @product = product.find(params[:id])
    @product.is_hidden = false
    @product.save
    redirect_to :back
  end

  def hide
    @product = product.find(params[:id])
    @product.is_hidden = true
    @product.save
    redirect_to :back
  end

12.reflect to model

app/models/product.rb
class product < ApplicationRecord
  def publish!
    self.is_hidden = false
    self.save
  end

  def hide!
    self.is_hidden = true
    self.save
  end
end  

revise app/controllers/admin/products_controller.rb

app/controllers/admin/products_controller.rb
def publish
    @product = product.find(params[:id])
    @product.publish!

    redirect_to :back
  end

  def hide
    @product = product.find(params[:id])

    @product.hide!

    redirect_to :back
  end

Comments

comments powered by Disqus