EExcel 丞燕快速查詢2

EExcel 丞燕快速查詢2
EExcel 丞燕快速查詢2 https://sandk.ffbizs.com/
顯示具有 sinatra 標籤的文章。 顯示所有文章
顯示具有 sinatra 標籤的文章。 顯示所有文章

justhost ruby sinatra activerecord

2015/05/10   快吐死!!被搞死!!

突然有一天,發現不能運作,查了老半天,發現是activerecord好像不能運作,但明明程式沒改,自行就不能連了.......

解決方式:請在database.yml  補上 socket: /tmp/mysql.sock

http://stackoverflow.com/questions/5499035/ruby-on-rails-3-cant-connect-to-local-mysql-server-through-socket-tmp-mysql-s

為何要這樣?不能理解,有人能告訴我嗎?

另外
用排程 Cron Jobs,每三十分鐘   touch ~/rails_apps/xxxxxxxxx/tmp/restart.txt
因為 有時候它會自動停止運作!



2014/08/21
====justhost===
第一步:先按官方的做
https://my.justhost.com/hosting/help/rails

重點一:請加subdomain,然後home目錄會有subdomain目錄,砍了,改成用ln,詳細請參考上面官方範例。記得做完要多確定路徑。

重點二:記得因為是用passenger,所以要在你的rails_app/xxxapp目錄下,建立兩個目錄:public和tmp目錄,tmp目錄下放restart.txt;

public不用管,上網查,只是passenger用
tmp很重要,你上傳更新後,要用ssh登入到app目錄下,執行touch ./tmp/restart.txt 這樣等五秒,更新的程式才會更新

如果覺得麻煩,可以放always_restart.txt,執行一次以後,就每次都會更新;取消方式,把檔案刪除即可。

重點三:請把.htaccess檔案放到public目錄,這超級重要!!

其它按上面的官方說明,把它做完就行了!

====sinatra=====
底下網站參考和準備
http://www.danneu.com/posts/15-a-simple-blog-with-sinatra-and-active-record-some-useful-tools/
http://stackoverflow.com/questions/16683903/sinatra-mysql-and-activerecord
http://www.sinatrarb.com/intro.html
http://qiita.com/u1_fukui/items/88c10d4d530ec6fbaaa1
http://recipes.sinatrarb.com/p/models/active_record

底下檔案放在同一個目錄底下:
1. Gemfile
# Gemfile
source :rubygems
gem "sinatra"
gem "activerecord"
gem "mysql2"
gem "shotgun"  --這無用**開發測試有用
gem "tux"  --超重要,但只限一開始,db確定連上就沒什麼用,但可以做db操作 **開發測試用


bundle install

2. database.yml 填一填
production:
  adapter: mysql2
  database:
  host: localhost
  username:
  password:
  encoding: utf8
  socket: /tmp/mysql.sock

3. config.ru
ENV['GEM_PATH'] = '/homex/xxxxxxx/ruby/gems:/lib/ruby/gems/1.9.3'

require File.expand_path '../app.rb', __FILE__
run Sinatra::Application

重點一:/homex/xxxx   請改成你申請的
重點二:要 ..  為什麼,因為passenger抓到是public,所以要退回上一層才看得到你寫的app.rb

4. app.rb 為什麼用這名字,記得跟你的目錄無關,跟在config.ru指定有關

http://sueboy.blogspot.com/2014/08/ruby-sinatra-activerecord.html
有說明了,直接用mysql2

require "sinatra"
require "active_record"
require 'mysql2'

ActiveRecord::Base.configurations = YAML.load_file('database.yml')
ActiveRecord::Base.establish_connection('production')

class Article < ActiveRecord::Base      
end

get '/' do
  content_type :json, :charset => 'utf-8'
  articles = Article.order("created_at DESC")
  articles.to_json(:root => false)
end

先用tux測是否有連到db,有錯誤訊息就是有設錯。
========以上完畢============

請輸入你的網址:xxxapp.xxx.com,有看到json輸出就是ok了!

因為是在遠端,ruby的語法錯誤會抓到,但是gem套件出錯的話,是看不到錯誤訊息......要用大腦算出來,或是在本地端測,但因為設定不太一樣,要先改一下

改那:
重點1:mysql設定.....改成development
重點2:config.ru 因為不是用passenger,所以../app.rb改成./app.rb
重點3:bundle install

先用tux測是否有連到db

ok,之後就開shotgun,這個在遠端執行後,試過是連不上。

tux非常好用,如果它是sinatra的套件,當執行時,他會對應activerecord,所以連不到就代表資料庫設定有錯了。



======RailsEnv====
設在.htaccess
RailsEnv production

設了之後,在程式內是拿不到值的.........

ruby sinatra admin login rack--two way

1.

http://www.sinatrarb.com/faq.html

require 'sinatra'

helpers do
  def protected!
    return if authorized?
    headers['WWW-Authenticate'] = 'Basic realm="Restricted Area"'
    halt 401, "Not authorized\n"
  end

  def authorized?
    @auth ||=  Rack::Auth::Basic::Request.new(request.env)
    @auth.provided? and @auth.basic? and @auth.credentials and @auth.credentials == ['admin', 'admin']
  end
end

get '/' do
  "Everybody can see this page"
end

get '/protected' do
  protected!
  "Welcome, authenticated client"
end



2.
http://www.highdots.com/forums/ruby-rails-talk/logging-out-rake-auth-basic-284054.html


LOGIN FORM (GET LOGIN METHOD)

<form method="post" action="/login">
<p><label>Username</label><input name="post[username]" /></p>
<p><label>Password</label><input name="post[password]" 
type="password"/></p>
<p><button type="submit">Login</button></p>
</form>


POST LOGIN METHOD

post '/login' do

if authenticate(params["post"]["username"], Digest::MD5.hexdigest(params["post"]["password"]))
    session[:user] = params["post"]["username"]
    flash[:notice] = "Login succeeded!"
    redirect '/admin'
else
    flash[:error] = "Login failed!"
    redirect '/login'
end

end


HELPER METHODS

# Authentication is hard-coded as there will only 1-3 users
def authenticate(username, password)

    if username == 'admin' and password == '[admin_password_in_MD5]'
        return true
    else
        return false
    end

end

# Protect pages
def login_required
    if session[:user]
        return true
    else
        redirect '/login'
        return false
    end
end

# Get the username of the logged in user
def current_user
    if session[:user]
        session[:user]
    end
end

# Verify if a user is logged in
def logged_in?
    !!session[:user]
end



使用activerecord注意

** tableize
rails console下,執行"Post".tableize 得到posts,table名稱

----------------------
sinatra 在tux下執行


** singularize 複數變單數
執行上方式同上

** pluralize 單數變複數
執行上方式同上

http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html

ruby sinatra activerecord

如果是用
require "sinatra"
require "active_record"

require 'mysql2'
用底下設定比較快
ActiveRecord::Base.configurations = YAML.load_file('database.yml')
ActiveRecord::Base.establish_connection('production')

用
require "sqlite3"
ActiveRecord::Base.establish_connection(
  :adapter => 'sqlite3',
  :database =>  'blog.db'
)

注意:database.yml 要像底下,關健字不一樣,就不能用

production:
  adapter: mysql2
  database:
  host: localhost
  username:
  password:
  encoding: utf8