ruby sinatra admin login rack--two way

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

2014-09-25 · 1 min · 173 words · Me

使用activerecord注意

** tableize rails console下,執行"Post".tableize 得到posts,table名稱 sinatra 在tux下執行 ** singularize 複數變單數 執行上方式同上 ** pluralize 單數變複數 執行上方式同上 http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html

2014-08-29 · 1 min · 16 words · Me

ruby on sinatra 正式開發上線 發現問題

基本上,很多人用 rank db:migrate, 看日本的範例是直接開mysql資料庫,那時候不懂,他的做法出乎意外,因為大部份的範例不是這樣,現在才知道為什麼。 rank db:create_migration NAME=xxx 建立後,再改xxx.rb檔,設完後,rank db:migrate就產生大問題了! 在開發環境,用sqlite3, 是的,可以用,但到正式mysql資料庫,能用,但編碼是litaxxxx ….不是UTF8;那在xxx.rb 文件內強制設定option create_table :tops, options: ‘DEFAULT CHARSET=utf8’ 當然就不發生錯誤,但回到sqllite3就錯了!因為sqlite3沒支援這語法……那這樣怎麼處理? 寫程式,因為xxx.rb,是ruby語法。 但這樣有比較快嗎? 還是直接把 cretae table schema和insert sql直接另外做文件,進mysql和sqlite3執行sql比較快…. mysql 當然更快是直接把table名稱改掉,新增 新的table; sqlite3 直接copy一個新的sqlite3,進去刪掉table更快… 也許是我不了解rank db,但目前的了解就是這樣子…. 另外有試著把mysql的db,需設成utf8,但租的主機有時候是不能亂改的…..哈,無解

2014-08-25 · 1 min · 34 words · Me

[轉]database.yml &, <<, *

http://stackoverflow.com/questions/6651275/what-do-the-mean-in-this-database-yml-file The & marks an alias for the node (in your example &defaultaliases the development node as “default”) references the aliased node with the name “default”. «: inserts the content of that node. test: &test «: *default default_env: «: *production

2014-08-24 · 1 min · 40 words · Me

sinatra mobile app cordova -- just for note

https://gauntface.com/blog/2014/02/05/cordova-web-best-practices-v2-0 http://spiritmachineblog.tumblr.com/post/41432461669/a-simple-openlayers-app-with-yeoman-sinatra-mongodb HTML5 Boilerplate Sass with Compass Modernizr http://thibaultdenizet.com/tutorial/cors-with-angular-js-and-sinatra/ 簡單說:發現現在要走moble app,要有yoeman的產生的網頁檔,或是 走Angular.js, jquerymobile, bootstrap三個來做。 新增、修改、刪除 走Restful,看上面範例。這樣產出來的網頁能適用 手機和平板。 但要把 程式轉成可以用cordova,要用Compass之類的,把所有的js, css, html5 三個檔。 http://vimeo.com/35998909 https://www.youtube.com/watch?v=e4yUTkva_FM https://www.youtube.com/watch?v=wVntVkRLR3M bit.ly/zen-ng-phonegap Angular UI部份 這看起來有點像官方Angular原生 http://angular-ui.github.io/bootstrap/ 這是另一個整合好Bootstrap and Angular JS http://mobileangularui.com/ 這是angular+ionic http://ionicframework.com/ http://ionicframework.com/docs/overview/#download http://ionicframework.com/docs/components/ ================== **SASS (Sass) **require jquery.mobile 21:13 http://demos.jquerymobile.com/1.2.0/ http://demos.jquerymobile.com/1.2.0/docs/pages/page-anatomy.html css style => data-role %meta{name: ‘viewport’, content: ‘width=device-width, initial-scal-1’) http://demos.jquerymobile.com/1.2.0/docs/lists/lists-collapsible.html **haml http://html2haml.heroku.com/ ===== phonegap 41:01 開一個android專案,但只是把網頁load進來…..真是簡單 www/index.html script include: phonegap-1.3.0.js onBodyLoad(){ document.addEventListener(“dviceready”, onDeviceready, false); windows.location.href = ‘http://localhost:3000’ } http://mobileangularui.com/demo/#/ http://ionicframework.com/ ============ https://www.youtube.com/watch?v=sCnGSOaaZFo http://jaydata.org/blog/angularjs-with-jaydata%E2%80%93-the-todo-example-with-the-itemstore-api ...

2014-08-24 · 2 min · 280 words · Me

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:

2014-08-21 · 1 min · 33 words · Me

ruby php implode

ostr = '{ "data": [' articles = Article.order("created_at DESC") ostr = ostr + articles.map{|f| ' ["' + f.title.chomp + '", "' + f.file_url + '"] '}.join(', ') ostr = ostr + "]}" map 會把activerecord取出來,設給f 然後組字串,最後加上join 這樣就像implode功能,效果很好~~

2014-08-21 · 1 min · 36 words · Me

ActiveRecord Migration API

http://blackanger.blog.51cto.com/140924/122472/ Migration API 不設定主鍵 create_table :goods, :id => false do |t| t.integer :good_id t.string :good_desc 改變主鍵 create_table :goods, :id => good_id do |t| t.string :good_desc end

2014-08-20 · 1 min · 26 words · Me

....Ruby on Rails 目標?? --2--

basic 終於結束了!往下一個。上一個步驟,最多問題rails3有些指令是rails4不能用,其中route中的match,現在要指定get或post,反正目前搞不清楚,所以只能自己手動加route,先能跑就好,以後比較熟,再好好研究,不然一直卡住,先往下學。 RESTful 應用程式 http://ihower.tw/rails4/restful.html ===== button_to ===== 如果用button_to,而產生route跟你要POST,後面補上 :method => :get <%= button_to ‘New event’, new_event_path, :method => :get%> 這樣就ok~ 解答來自下面連結 http://stackoverflow.com/questions/5221227/rails-button-to-fails-with-path-doesnt-exist-for-a-path-that-exists

2014-07-04 · 1 min · 23 words · Me

....Ruby on Rails 目標?? --3--

http://ihower.tw/rails4/environments-and-bundler.html Rails 啟動與應用程式設定 =>/config/application.rb

2014-07-04 · 1 min · 4 words · Me

....Ruby on Rails 目標??

http://vimeo.com/35998909 參考:http://clickonchris.com/2014/02/beerdogging-mobile-phonegap-angularjs/ 第一步:裝RVM,保持乾淨的環境,不要用原生環境 http://dylandychat.blogspot.com/2013/11/ubuntu-rvm-ruby-on-rails.html http://blog.eddie.com.tw/2011/04/08/rvm-and-gemsets/ http://www.openfoundry.org/tw/tech-column/8513-rvm-ruby-environment-version-manager http://ihower.tw/rails3/advanced-installation.html# Ubuntu Desktop部份 看完,做完,心裡就有數了。 http://my.oschina.net/zlLeaf/blog/149650 上网查了一下,说要在Gemfile上加上gem ’execjs’ 和 gem ’therubyracer’,但是就不告诉我到底Gemfile在哪里啊,那叫我这种菜鸟去哪里找这个文件喔,然后我把整个.rvm文件都找遍了,终于知道了原来Gemfile在项目文件夹里面,不再.rvm里。。。。。。 所以我在projectName(刚刚新建的那个项目)里面的Gamefile加上gem ’execjs’ 和 gem ’therubyracer’(在END之前加啊,相信应该都看得懂的),然后运行bundle install之后就可以运行rails server启动服务器了,连关闭都正常了,太高兴了 另外 重開shell後,無效問題 http://www.openfoundry.org/tw/tech-column/8513-rvm-ruby-environment-version-manager https://rvm.io/rvm/install 方法一:執行 source ~/.rvm/scripts/rvm 方法二:Put this in your ~/.profile or ~/.bashrc: [[ -s “$HOME/.rvm/scripts/rvm” ]] && . “$HOME/.rvm/scripts/rvm” 按 http://ihower.tw/rails3/firststep.html 練習 http://ihower.tw/rails3/ruby.html http://ihower.tw/rails3/basic.html ====basic=== match ‘:controller(/:action(/:id(.:format)))’ 這無用,自己手動加route » get “events” => “events#index” get “events/new” => “events#new” post “events/create” => “events#create” ====basic=== @event = Event.new(params[:event]) 這裡在rails 4.0有變化 http://stackoverflow.com/questions/17335329/activemodelforbiddenattributeserror-when-creating-new-user def create @event = Event.new(user_params) …. ...

2014-06-30 · 1 min · 109 words · Me