ADD frendly_id to homes

This commit is contained in:
Jasder 2019-10-16 10:31:28 +08:00
parent 30ff3234aa
commit ea1f3ed5bf
6 changed files with 36 additions and 9 deletions

View File

@ -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

View File

@ -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

View File

@ -11,5 +11,4 @@
</p>
<%= link_to 'Edit', edit_home_path(@home) %> |
<%= link_to 'Back', homes_path %>

View File

@ -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

View File

@ -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

View File

@ -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"