From ea1f3ed5bf5b8f7c64e2bda2bf892d2096e306df Mon Sep 17 00:00:00 2001 From: Jasder <2053003901@@qq.com> Date: Wed, 16 Oct 2019 10:31:28 +0800 Subject: [PATCH] ADD frendly_id to homes --- app/controllers/homes_controller.rb | 11 +++++++---- app/models/home.rb | 14 +++++++++++++- app/views/homes/show.html.erb | 1 - config/routes.rb | 4 ++-- .../20191016015946_add_friendly_id_to_homes.rb | 10 ++++++++++ db/schema.rb | 5 ++++- 6 files changed, 36 insertions(+), 9 deletions(-) create mode 100644 db/migrate/20191016015946_add_friendly_id_to_homes.rb diff --git a/app/controllers/homes_controller.rb b/app/controllers/homes_controller.rb index a9aceab3e..78595c320 100644 --- a/app/controllers/homes_controller.rb +++ b/app/controllers/homes_controller.rb @@ -1,6 +1,8 @@ class HomesController < ApplicationController layout 'base_introduce' + before_filter :find_home, :except => [:index, :new, :create] + # GET /homes # GET /homes.json def index @@ -15,7 +17,6 @@ class HomesController < ApplicationController # GET /homes/1 # GET /homes/1.json def show - @home = Home.find(params[:id]) respond_to do |format| format.html # show.html.erb @@ -36,7 +37,6 @@ class HomesController < ApplicationController # GET /homes/1/edit def edit - @home = Home.find(params[:id]) end # POST /homes @@ -58,7 +58,6 @@ class HomesController < ApplicationController # PUT /homes/1 # PUT /homes/1.json def update - @home = Home.find(params[:id]) respond_to do |format| if @home.update_attributes(params[:home]) @@ -74,7 +73,6 @@ class HomesController < ApplicationController # DELETE /homes/1 # DELETE /homes/1.json def destroy - @home = Home.find(params[:id]) @home.destroy respond_to do |format| @@ -82,4 +80,9 @@ class HomesController < ApplicationController format.json { head :no_content } end end + + private + def find_home + @home = Home.find_by_frendly_id! params[:id] + end end diff --git a/app/models/home.rb b/app/models/home.rb index e03306c07..ba8eeb436 100644 --- a/app/models/home.rb +++ b/app/models/home.rb @@ -4,9 +4,21 @@ class Home < ActiveRecord::Base has_one :cover, as: :coverable, dependent: :destroy has_many :banners, dependent: :destroy - validates_presence_of :name, :content + validates_presence_of :name, :content, :frendly_id + validates_format_of :friendly_id, :with => /\A[a-z0-9\-]+\z/ + + before_validation :generate_friendly_id, :on => :create def more_content? content.html_safe.length > Home::SHOW_CONTENT_LENGTH end + + def to_param + self.frendly_id + end + + private + def generate_frendly_id + self.frendly_id ||= SecureRandom.uuid + end end diff --git a/app/views/homes/show.html.erb b/app/views/homes/show.html.erb index df3251e3a..572001f85 100644 --- a/app/views/homes/show.html.erb +++ b/app/views/homes/show.html.erb @@ -11,5 +11,4 @@

-<%= link_to 'Edit', edit_home_path(@home) %> | <%= link_to 'Back', homes_path %> diff --git a/config/routes.rb b/config/routes.rb index 9d9b91383..dee7fe2a8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -515,8 +515,8 @@ RedmineApp::Application.routes.draw do get "praise_tread/tread_plus" #end - root :to => 'homes#index', :as => 'home' - resources :homes, only: [:show] + root :to => 'homes#index' + resources :homes, only: [:show, :index] # added by longjun match 'welcome/contest', :via => :get # end longjun diff --git a/db/migrate/20191016015946_add_friendly_id_to_homes.rb b/db/migrate/20191016015946_add_friendly_id_to_homes.rb new file mode 100644 index 000000000..537c6a3d1 --- /dev/null +++ b/db/migrate/20191016015946_add_friendly_id_to_homes.rb @@ -0,0 +1,10 @@ +class AddFriendlyIdToHomes < ActiveRecord::Migration + def change + add_column :homes, :frendly_id, :string + add_index :homes, :frendly_id, :unique => true + + Home.find_each do |home| + home.update_attributes(:frendly_id => SecureRandom.uuid) + end + end +end diff --git a/db/schema.rb b/db/schema.rb index fbdac627b..1771ca9c5 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20191015090928) do +ActiveRecord::Schema.define(:version => 20191016015946) do create_table "activities", :force => true do |t| t.integer "act_id", :null => false @@ -1165,8 +1165,11 @@ ActiveRecord::Schema.define(:version => 20191015090928) do t.integer "users_count", :default => 0 t.integer "projects_count", :default => 0 t.integer "practical_training_projects_count", :default => 0 + t.string "frendly_id" end + add_index "homes", ["frendly_id"], :name => "index_homes_on_frendly_id", :unique => true + create_table "homework_attaches", :force => true do |t| t.integer "bid_id" t.integer "user_id"