跳到主要內容

發表文章

目前顯示的是有「Ruby on Rails」標籤的文章

How to attach a file/image in your data? Using paperclip in a rails application.

在 rails 裡有一個很好用的夾檔套件paperclip,今天就要來簡單介紹paperclip怎麼用. 首先要利用imagemagick套件,讓你的rails專案具有處理影像的能力. Mac OS X $ brew install imagemagick  Linux distribution $ sudo apt-get install imagemagick -y 然後在Gemfile中引用paperclip. # Gemfile ... gem "paperclip", git: "git://github.com/thoughtbot/paperclip.git" ... 記得bundle一下. $ bundle install 接下來利用scaffold製作一個範例,在這個範例中我們要用paperclip幫這個使用者加上照片,paperclip會將照片資料記錄起來並將照片上傳到public資料夾內. $ rails generate scaffold User name:string email:string tel:string  生成附檔資料表 $ rails generate paperclip user avatar 修改 class AddAvatarToUsers < ActiveRecord::Migration[5.2]   def up    add_attachment :users, :avatar   end   def down    remove_attachment :users, :avatar   end end 讓user model知道怎麼處理夾檔,可以上傳的檔案格式容量限制等都可以寫在這裡. # app/models/user.rb  class User < ActiveRecord::Base  has_attached_file :avatar, styles: { medium: "300x300", thumb: "100x100" }   v...

undefined method `for' for #<' Devise::ParameterSanitizer:0x00007f0e320f79d0 '>

 NoMethodError in Devise::RegistrationsController#edit 用Devise套件作為系統的帳號管理功能,結果在這個檔案出現錯誤. app/controller/application_controller.rb class ApplicationController < ActionController::Base   # Prevent CSRF attacks by raising an exception.   # For APIs, you may want to use :null_session instead.   protect_from_forgery with: :exception   before_action :configure_permitted_parameters , if: :devise_controller ?   protected   def configure_permitted_parameters     devise_parameter_sanitizer . for ( :sign_up ) { | u | u.permit( :username , :email , :password , :password_confirmation , :remember_me , :avatar , :avatar_cache ) }     devise_parameter_sanitizer . for ( :account_update ) { | u | u.permit( :username , :password , :password_confirmation , :current_password , :avatar ) }   end end   只要將for修正成permit,就可以了. def configure_permitted_parameters    devise_parameter_sanitizer .permit ( :sign_up ) ...

How to use docker compose build a ruby on rails container?

在推Ruby on rails協同開發架構時,發現每個人的電腦都不同,有的是windows、有的是linux、有的則是mac,單單為了要讓大家能一起開始,就傷透腦筋,為了解決開發環境一致性的問題,我們採用了docker,想法是:如果有一個能跨越自身的電腦作業系統版本,大家都能共用的環境,如同VM,我們不就可以解決這個問題了;所以有了這個專案。 Github網址: https://github.com/tyRoRteam/dev_docker 如何使用 安裝docker 請先到 Docker官網  安裝適合電腦的版本, 使用windows的朋友需注意必須windows 10 professional版本才裝得起來。 Download git repo $ git clone https://github.com/tyRoRteam/dev_docker.git  Build and Run RoR development docker container $ cd dev_docker $ docker-compose up -d  Login to container  $ ssh -X -p 10022 user@localhost (default username/password: user/user) docker-compose.yml 設定檔 version: '3'  services:  ror_dev:   build: ror_dev/   ports:    - "10022:22"    - "10080:3000"   volumes:    - ./user_home:/home/user    - ./configs/sshd_config:/etc/ssh/sshd_config 在設定檔中,我們開通了本機與container兩個port對接,本機的10022連接container的22 port,讓ssh可以連得進去,另一個就是本機的10080連接container的3000 port,因為rail...

建立自己第一個gem

1. 先要有https://rubygems.org/帳號 2. 安裝環境必要套件 $ gem update --system $ gem install rubygems-update Fetching: rubygems-update-2.7.6.gem (100%) Successfully installed rubygems-update-2.7.6 Parsing documentation for rubygems-update-2.7.6 Installing ri documentation for rubygems-update-2.7.6 Done installing documentation for rubygems-update after 28 seconds 1 gem installed $ update_rubygems 3. 建立gem資料夾 (voteable_shawn) $mkdir voteable_shawn $cd voteable_shawn $mkdir lib 4. 設定檔 voteable_shawn/voteable_shawn.gemspec Gem::Specification.new do |s|   s.name        = 'voteable_shawn'   s.version     = '0.0.0'   s.licenses    = ['MIT']   s.summary     = "This is a voteable gem example!"   s.description = "The voting gem!"   s.authors     = ["Shawn"]   s.email       = 'shawnkuo67@gmail.com'   s.files       = ["lib/vote...

解決Ruby升級後發生的has_many NoMethodError: undefined method `name' for nil:NilClass問題

rvm 2.1.4跑起來正常的專案,升級rvm 2.2.2就噴下面錯誤訊息要怎麼修正它呢? https://github.com/shawnkaku/postit-solution.git 補充:下載後要先bundle update json sqlite3 感謝網友 蔡鴻銘 指導 https://github.com/....../active_scaffold/issues/380...... 升級到rails 4.1.16 + ruby 2.2.4 問題就消失了 Step 1:安裝rvm 2.2.4 $ rvm install 2.2.4 $ rvm use 2.2.4 Using /Users/shawnkuo/.rvm/gems/ruby-2.2.4 Step 2: 修改Gemfile source 'https://rubygems.org' ruby '2.2.4' gem 'rails', '4.1.16' Step 3:升級專案套件 $ ruby -v ruby 2.2.4p230 (2015-12-16 revision 53155) [x86_64-darwin17] $ bundle install Fetching gem metadata from https://rubygems.org/.......... Fetching gem metadata from https://rubygems.org/. Resolving dependencies... Bundler could not find compatible versions for gem "activesupport":   In snapshot (Gemfile.lock):     activesupport (= 4.0.0)   In Gemfile:     sass-rails (~> 4.0.0) was resolved to 4.0.0, which depends on       sprockets-rails (~> 2.0.0) was resolved t...

Rails 網站不顯示id的方法

Add Gem. (/Gemfile) 加 gem 到 Gemfile 中 gem 'friendly_id', '~> 5.1.0' gem 'babosa' 安裝套件 bundle install 建立Table ($rails c) rails g friendly_id rails g migration add_slug_to_posts slug:string:uniq rake db:migrate 修改Model (app/models/post.rb) class Post < ActiveRecord::Base   belongs_to :user, foreign_key: :user_id   has_many :comments   has_many :post_categories   has_many :categories, through: :post_categories   has_many :votes, as: :voteable   validates :title, presence: true, length: {minimum: 5}   extend FriendlyId   friendly_id :id, use: :slugged   # 原本是input.to_s.parameterize,但是parameterize只支援   # 英文跟數字,所以改用babosa的to_slug   def normalize_friendly_id(input)     input.to_s.to_slug.normalize.to_s   end   def should_generate_new_friendly_id?     slug.blank? || id_changed?   # slug 為 nil 或 name...

How to set Ruby on Rails Environment in mac (mac OS)?

1. install xcode in apple App Stor 2. curl -L https://get.rvm.io | bash -s stable 3. rvm -v 4. echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function' >> ~/.zshrc 5. rvm list known 6. rvm install 2.5.1 7. ruby -v 8. rvm rubygems current 9. gem -v 10. $ brew install git 11. $ git config --global user.name "YourName" 12. $ git config --global user.email "yourmail@gmail.com"  13. gem install bundler 14. gem install rails

How to set Ruby on Rails Environment in Linux (Ubuntu)?

Update your package manager first: $ sudo apt-get update This must finish without error or the following step will fail. Install  Curl : $ sudo apt-get install curl You’ll use Curl for installing  RVM . $ \curl -L https://get.rvm.io | bash vi ~/.bashrc # This loads RVM into a shell session. [[ - s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" restart terminal. $ rvm get stable --autolibs=enable $ rvm install ruby $ rvm use 2.5.1 --default Nodejs $ sudo apt-get install nodejs $ gem install bundler $ gem install nokogiri $ gem install rails $ rails -v $ rails new AppName $ cd AppName $ rails s

04 Ruby on Rails 使用者認證機制

Getting Started with Rails 04 Ruby on Rails 使用者認證機制 每個網站幾乎都有使用者認證機制, Ruby on Rails 已經把這樣的機制做成套件了,而且每個 Ruby on Rails 的教學網站都有教怎麼做,所以我們也來依樣畫葫蘆一下這個使用者認證機制 -- devise 。 1.      引入 devise 套件 廢話依舊不多說,直接 step by step 跟我一起這樣做吧。 1.       修改 Gemfile $vi Gemfile 加入 devise 套件 Source ‘https://rubygems.org’ gem ‘devise’ 2.       透過 rails g 指令將 devise 套件安裝好 $rails g devise:install $rails g devise User $rake db:migrate 3.       在 /views/comments/ 中建立 _usr_signin_bar.erb $vi /views/comments/ _usr_signin_bar.erb 4.       最後修改 /views/layouts/application.html.erb 加上 <%= render :partial => “comments/user_signin_bar” %> 5.       驗證測試 01 用Ruby on Rails純手工打造一個包含CRUD的RESTful應用程式 02 用Ruby on Rail...

03 用Ruby on Rails純手工打造一個具從屬關係的[討論區] (2)

Getting Started with Rails 用 Ruby on Rails 純手工打造一個具從屬關係的 [ 討論區 ] (2) 我們很有耐心的把一個討論區的雛型建構出來了,但是這個討論區的程式碼其實可以再有效率的精簡,這個章節要討論的就是 重構 ,讓重複的程式碼簡化,程式設計師的時間很寶貴的不要做相同的事。 1.      重構 建構完成一個功能重新檢視會發現重複性質的部分其實滿多的,而且我們的城市可以再精簡一點,一方面變得好看一點、也可以變得好維護一些,當然慢慢的形成精簡化的思考模式後,將來會很直覺的以模組化的方式來開發系統了。 首先利用的就是 render 方法。 下面的語法指的是在檔案中,調用與原 view 路徑相同的 _form.html.erb 檔案。 <%= render ‘form’ %> 舉例來說,如果在 /views/posts/ 資料夾中有 edit.html.erb 與 _PostList.html.erb 兩個檔案,在 edit.html.erb 中要 調用 _PostList.html.erb 做法如下。 <%= render ‘ PostListb ’ %> 好了,說明完後,接下來就可以實作了。 1.       檢視 /views/posts/new.html.erb 與 /views/posts/edit.html.erb 原 /views/posts/new.html.erb 檔案內容為: 原 /views/posts/edit.html.erb 檔案內容為: 可以發現 /views/posts/new.html.erb 檔案內容第 3~8 行與 /views/posts/edit.html.erb 檔案內容第 3~15 行作用相同,所以我們將相同部分獨立出來製作成一個新的檔案 _ePostList.html.erb 2.       修改 /views/posts/ne...

02 用Ruby on Rails純手工打造一個具從屬關係的[討論區] (1)

Getting Started with Rails 用 Ruby on Rails 純手工打造一個具從屬關係的 [ 討論區 ] 在上一個範例中已經可以用純手工成功的完成簡易的 CRUD 應用程式,接下來要透過打造一個複雜一點的應用程式 [ 討論區 ] ,來練習具有從屬關係的資料結構,也順便練一練 CRUD ,一定要透過不斷練習讓 CRUD 變得很自然。 討論區的結構中有許多 Po 文 (Post) ,每個 Po 文會有許多評論 (Comment) ,這樣的從屬關係非常適合當成範例來說明,所以我們的資料結構如下圖, Comment 透過 post_id 與 post 做關聯。 1.      has_many 與 belongs_to 的 convention 上面這樣的結構我們可以說一個 post 可以具有 has many 的 comment 關係,而 comment 與 post 有著 belongs to 的關係。 l           has_one l           has_many l           belongs_to l           has_many :through 好了,說明完我們的目標結構後,接下來就可以實作了。 1.       建立一個專案 $rails new sample042 $cd sample042 2.       建立一個名為 post 的 model ,其中包含 name, title 與 content 三個欄位 $rails g model po...