熱門文章

顯示具有 RoR 標籤的文章。 顯示所有文章
顯示具有 RoR 標籤的文章。 顯示所有文章

2017年2月20日 星期一

常見的 Ruby on Rails 工程師面試問題

常見的 Ruby on Rails 工程師面試問題

收錄一些業界常見/常問的面試問題和翻譯解答

解答不見得完整和精確,但在面試的環境下回答應該是足夠的

有需要改正或有什麼要新增 / 補充的地方也請大家發 PR

開始閱讀

2016年8月15日 星期一

[rails]form-tag

>>index.html.erb

<%= form_tag(groups_path, method: "get") do %>
  <%= label_tag(:q) %>
  <%= text_field_tag(:q) %>
  <%= submit_tag("搜索") %>
<% end %>
<%= @a %>
</div>


groups_path  :處理的路徑
:q 將被存在 controller 裡相對路徑的 method 裡
此範例是 groupscontroller.rb的 index 裡如下

def index
@groups = Group.all
a = params[:q]
end

對網頁應用程式來說,表單是非常重要的用戶輸入介面。Rails在這方面也提供了很多好用的Helper方法。基本上,Rails處理表單分成兩種類型:
一種是對應到Model物件的新增、修改,我們會使用form_for這個Helper。它的好處在於透過傳入Model物件,可以在修改的時候自動幫你將預設值帶入。例如我們已經在Part1使用過的event表單:
<%= form_for @event do |f| %>
    <%= f.text_field :name %>
    <%= f.submit %>
<% end %>
另一種是就是沒有對應Model的表單,我們使用form_tag這個方法。例如:
<%= form_tag "/search" do %>
    <%= text_field_tag :keyword %>
    <%= submit_tag %>
<% end %>
form_for有些類似,但是其中不需要傳Block變數f,其中的欄位Helper需要多加_tag結尾。不像form_for的欄位名稱一定要是Model的屬性之一,在form_tag之中的欄位名稱則完全不受限。


form_tag 是啥?跟 form_for 有什麼不一樣?

Adler @ 2015-07-18

2016年8月9日 星期二

2016年7月28日 星期四

[RoR]Address already in use - bind(2) (Errno::EADDRINUSE)

Address already in use - bind(2) (Errno::EADDRINUSE)

當出現錯誤 already in use:


bundle exec puma -C config/puma.rb
[23699] Puma starting in cluster mode...
[23699] * Version 2.11.3 (ruby 2.0.0-p353), codename: Intrepid Squirrel
[23699] * Min threads: 5, max threads: 5
[23699] * Environment: development
[23699] * Process workers: 2
[23699] * Preloading application
Jdbc-MySQL is only for use with JRuby
[23699] * Listening on tcp://0.0.0.0:3000
/.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/binder.rb:210:in `initialize': Address already in use - bind(2) (Errno::EADDRINUSE)
    from /.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/binder.rb:210:in `new'
    from /Users/lexi87/.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/binder.rb:210:in `add_tcp_listener'
    from /.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/binder.rb:96:in `block in parse'
    from /.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/binder.rb:82:in `each'
    from /.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/binder.rb:82:in `parse'
    from /.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/runner.rb:119:in `load_and_bind'
    from /.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/cluster.rb:302:in `run'
    from /.rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/lib/puma/cli.rb:216:in `run'
    from /rvm/gems/ruby-2.0.0-p353/gems/puma-2.11.3/bin/puma:10:in `<top (required)>'
    from /.rvm/gems/ruby-2.0.0-p353/bin/puma:23:in `load'
    from /.rvm/gems/ruby-2.0.0-p353/bin/puma:23:in `<main>'
    from /.rvm/gems/ruby-2.0.0-p353/bin/ruby_executable_hooks:15:in `eval'
    from /.rvm/gems/ruby-2.0.0-p353/bin/ruby_executable_hooks:15:in `<main>'


解決方法:

To kill the puma process first run 
    lsof -wni tcp:3000 
to show what is using port 3000. Then use the PID that comes with the result to run the kill process. 
For example after running lsof -wni tcp:3000 you might get something like 
    COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
    ruby    3366 dummy    8u  IPv4  16901      0t0  TCP 127.0.0.1:3000 (LISTEN)
Now run kill -9 3366
Should resolve the issue

2016年7月12日 星期二

[Ruby]bulid a IG app

https://www.devwalks.com/lets-build-instagram-in-rails-part-1/


https://haml2erb.org   haml to html


在實作時出現錯誤

Please help with the Paperclips!

First things first, as per the docs, you’ll need imagemagick running on your development computer. Set it up as per the instructions here.

--------------------------------------------------------------------------------
Set the MAGICK_HOME environment variable to the path where you extracted the ImageMagick files. For example:
 export MAGICK_HOME="$HOME/ImageMagick-7.0.2"
If the bin subdirectory of the extracted package is not already in your executable search path, add it to your PATH environment variable. For example:
export PATH="$MAGICK_HOME/bin:$PATH"
Set the DYLD_LIBRARY_PATH environment variable:
export DYLD_LIBRARY_PATH="$MAGICK_HOME/lib/"
Finally, to verify ImageMagick is working properly, type the following on the command line:
magick logo: logo.gifidentify logo.gifdisplay logo.gif




--------------------------------------------------------------------------------
後來忽略,回到 https://www.devwalks.com/lets-build-instagram-in-rails-part-1/


Next we’ll add the paperclip gem to our gemfile as per their docs.
gem 'paperclip', '~> 4.2' #at the writing of this post  

Read the next bit of the docs, what does it ask you to do now?

bundle install









再上傳檔案時發生

Photo Could not run the identify command. Please install ImageMagick.

解決,
You need to change the files production.rb and development.rb which are in:
config/environments:
# Paperclip config:
Paperclip.options[:image_magick_path] = "/opt/ImageMagick/bin"
Paperclip.options[:command_path] = "/opt/ImageMagick/bin"
Just add those two lines just before the last line and restart the server.
In MacOSX system, if you are using another location, just put the right PATH there.

我的路徑是 :
Paperclip.options[:command_path] = "/Users/QJpioneer/ImageMagick-7.0.1/bin/"  Paperclip.options[:image_magick_path] = "/Users/QJpioneer/ImageMagick-7.0.1/bin/"



上傳檔案後
似乎沒有吃進去圖檔.....

用rails c 進去看




看伺服器端

Started POST "/posts" for ::1 at 2016-07-13 07:15:07 +0800Processing by PostsController#create as HTML  Parameters: {"utf8"=>"✓", "authenticity_token"=>"Qyu7WWiAJR5AV0MuzIEzfbXvoeYVDxcla+wGaWm92bwA9QScqJf+hZ3aJkvP1OutO5W3+Lgbh4AjsCtDKukVKg==", "post"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x007fc281b10ff8 @tempfile=#<Tempfile:/var/folders/js/038lfs452n362y3xc7phyz100000gn/T/RackMultipart20160713-54694-1qdykz.jpg>, @original_filename="Country life.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"post[image]\"; filename=\"Country life.jpg\"\r\nContent-Type: image/jpeg\r\n">, "caption"=>"123"}, "commit"=>"Create Post"}Command :: PATH=/Users/QJpioneer/ImageMagick-7.0.1/bin/:$PATH; file -b --mime '/var/folders/js/038lfs452n362y3xc7phyz100000gn/T/993316952cab27089c21a4f6fdd5299020160713-54694-1fsypu2.jpg'Command :: PATH=/Users/QJpioneer/ImageMagick-7.0.1/bin/:$PATH; identify -format '%wx%h,%[exif:orientation]' '/var/folders/js/038lfs452n362y3xc7phyz100000gn/T/b2aa763204471d9d58ed3488ce58a26b20160713-54694-1129s9z.jpg[0]' 2>/dev/null[paperclip] An error was received while processing: #<Paperclip::Errors::NotIdentifiedByImageMagickError: Paperclip::Errors::NotIdentifiedByImageMagickError>   (0.1ms)  begin transactionCommand :: PATH=/Users/QJpioneer/ImageMagick-7.0.1/bin/:$PATH; file -b --mime '/var/folders/js/038lfs452n362y3xc7phyz100000gn/T/993316952cab27089c21a4f6fdd5299020160713-54694-1e7exev.jpg'   (0.1ms)  rollback transactionRedirected to http://localhost:3000/postsCompleted 302 Found in 143ms (ActiveRecord: 0.3ms)Started GET "/posts" for ::1 at 2016-07-13 07:15:08 +0800Processing by PostsController#index as HTML  Post Load (0.2ms)  SELECT "posts".* FROM "posts"  Rendered posts/index.html.erb within layouts/application (1.1ms)Completed 200 OK in 32ms (Views: 31.3ms | ActiveRecord: 0.2ms)





嘗試解決:
reinstall homebrew  http://brew.sh <-官方

安裝homebrew
➜  IGapp git:(master) /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
It appears Homebrew is already installed. If your intent is to reinstall you
should do the following before running this installer again:
    ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"

The current contents of /usr/local are bin Cellar CODEOFCONDUCT.md CONTRIBUTING.md etc git heroku include lib Library LICENSE.txt opt README.md sbin share SUPPORTERS.md .git .gitignore

顯示已有homebrew必須執行以下來解除安裝:
 ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"


安裝最新:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
安裝途中須按enter繼續,並輸入密碼,安裝完成。


嘗試  brew install imagemagick

➜  IGapp git:(master) brew install imagemagick
==> Installing dependencies for imagemagick: xz, libtool, jpeg, libpng, libtiff, free
==> Installing imagemagick dependency: xz
==> Downloading https://homebrew.bintray.com/bottles/xz-5.2.2.el_capitan.bottle.tar.gz
######################################################################## 100.0%
==> Pouring xz-5.2.2.el_capitan.bottle.tar.gz
🍺  /usr/local/Cellar/xz/5.2.2: 91 files, 1.4M
==> Installing imagemagick dependency: libtool
==> Downloading https://homebrew.bintray.com/bottles/libtool-2.4.6.el_capitan.bottle.tar.gz
######################################################################## 100.0%
==> Pouring libtool-2.4.6.el_capitan.bottle.tar.gz
==> Caveats
In order to prevent conflicts with Apple's own libtool we have prepended a "g"
so, you have instead: glibtool and glibtoolize.
==> Summary
🍺  /usr/local/Cellar/libtool/2.4.6: 70 files, 3.7M
==> Installing imagemagick dependency: jpeg
==> Downloading https://homebrew.bintray.com/bottles/jpeg-8d.el_capitan.bottle.2.tar.gz
######################################################################## 100.0%
==> Pouring jpeg-8d.el_capitan.bottle.2.tar.gz
🍺  /usr/local/Cellar/jpeg/8d: 19 files, 713.7K
==> Installing imagemagick dependency: libpng
==> Downloading https://homebrew.bintray.com/bottles/libpng-1.6.23.el_capitan.bottle.tar.gz
######################################################################## 100.0%
==> Pouring libpng-1.6.23.el_capitan.bottle.tar.gz
🍺  /usr/local/Cellar/libpng/1.6.23: 25 files, 1.2M
==> Installing imagemagick dependency: libtiff
==> Downloading https://homebrew.bintray.com/bottles/libtiff-4.0.6_1.el_capitan.bottle.tar.g
######################################################################## 100.0%
==> Pouring libtiff-4.0.6_1.el_capitan.bottle.tar.gz
🍺  /usr/local/Cellar/libtiff/4.0.6_1: 261 files, 3.4M
==> Installing imagemagick dependency: freetype
==> Downloading https://homebrew.bintray.com/bottles/freetype-2.6.5.el_capitan.bottle.tar.gz
######################################################################## 100.0%
==> Pouring freetype-2.6.5.el_capitan.bottle.tar.gz
🍺  /usr/local/Cellar/freetype/2.6.5: 61 files, 2.5M
==> Installing imagemagick
==> Downloading https://homebrew.bintray.com/bottles/imagemagick-6.9.5-2.el_capitan.bottle.t
######################################################################## 100.0%
==> Pouring imagemagick-6.9.5-2.el_capitan.bottle.tar.gz
🍺  /usr/local/Cellar/imagemagick/6.9.5-2: 1,464 files, 22.5M


可以執行
➜  IGapp git:(master) which convert
/usr/local/bin/convert

不過出現另一個問題

➜  IGapp git:(master) rake db:migrate
zsh: /usr/local/bin/rake: bad interpreter: /usr/local/opt/ruby/bin/ruby: no such file or directory
rake aborted!
cannot load such file -- bundler/setup
/Users/QJpioneer/railsbridge/IGapp/config/boot.rb:3:in `<top (required)>'
/Users/QJpioneer/railsbridge/IGapp/config/application.rb:1:in `<top (required)>'
/Users/QJpioneer/railsbridge/IGapp/Rakefile:4:in `<top (required)>'
(See full trace by running task with --trace)


➜  IGapp git:(master) bundle install
zsh: /usr/local/bin/bundle: bad interpreter: /usr/local/opt/ruby/bin/ruby: no such file or directory


➜  IGapp git:(master) bin/rails s
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- bundler (LoadError)
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /Users/QJpioneer/railsbridge/IGapp/bin/spring:8:in `<top (required)>'
from bin/rails:3:in `load'
from bin/rails:3:in `<main>'

嘗試另一篇來解決


[Ruby]bulid a IG app

https://www.devwalks.com/lets-build-instagram-in-rails-part-1/


https://haml2erb.org   haml to html


在實作時出現錯誤

Please help with the Paperclips!

First things first, as per the docs, you’ll need imagemagick running on your development computer. Set it up as per the instructions here.

--------------------------------------------------------------------------------
Set the MAGICK_HOME environment variable to the path where you extracted the ImageMagick files. For example:
 export MAGICK_HOME="$HOME/ImageMagick-7.0.2"
If the bin subdirectory of the extracted package is not already in your executable search path, add it to your PATH environment variable. For example:
export PATH="$MAGICK_HOME/bin:$PATH"
Set the DYLD_LIBRARY_PATH environment variable:
export DYLD_LIBRARY_PATH="$MAGICK_HOME/lib/"
Finally, to verify ImageMagick is working properly, type the following on the command line:
magick logo: logo.gifidentify logo.gifdisplay logo.gif




--------------------------------------------------------------------------------
後來忽略,回到 https://www.devwalks.com/lets-build-instagram-in-rails-part-1/


Next we’ll add the paperclip gem to our gemfile as per their docs.
gem 'paperclip', '~> 4.2' #at the writing of this post  

Read the next bit of the docs, what does it ask you to do now?

bundle install









再上傳檔案時發生

Photo Could not run the identify command. Please install ImageMagick.

解決,
You need to change the files production.rb and development.rb which are in:
config/environments:
# Paperclip config:
Paperclip.options[:image_magick_path] = "/opt/ImageMagick/bin"
Paperclip.options[:command_path] = "/opt/ImageMagick/bin"
Just add those two lines just before the last line and restart the server.
In MacOSX system, if you are using another location, just put the right PATH there.

我的路徑是 :
Paperclip.options[:command_path] = "/Users/QJpioneer/ImageMagick-7.0.1/bin/"  Paperclip.options[:image_magick_path] = "/Users/QJpioneer/ImageMagick-7.0.1/bin/"



上傳檔案後
似乎沒有吃進去圖檔.....

用rails c 進去看




看伺服器端

Started POST "/posts" for ::1 at 2016-07-13 07:15:07 +0800Processing by PostsController#create as HTML  Parameters: {"utf8"=>"✓", "authenticity_token"=>"Qyu7WWiAJR5AV0MuzIEzfbXvoeYVDxcla+wGaWm92bwA9QScqJf+hZ3aJkvP1OutO5W3+Lgbh4AjsCtDKukVKg==", "post"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x007fc281b10ff8 @tempfile=#<Tempfile:/var/folders/js/038lfs452n362y3xc7phyz100000gn/T/RackMultipart20160713-54694-1qdykz.jpg>, @original_filename="Country life.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"post[image]\"; filename=\"Country life.jpg\"\r\nContent-Type: image/jpeg\r\n">, "caption"=>"123"}, "commit"=>"Create Post"}Command :: PATH=/Users/QJpioneer/ImageMagick-7.0.1/bin/:$PATH; file -b --mime '/var/folders/js/038lfs452n362y3xc7phyz100000gn/T/993316952cab27089c21a4f6fdd5299020160713-54694-1fsypu2.jpg'Command :: PATH=/Users/QJpioneer/ImageMagick-7.0.1/bin/:$PATH; identify -format '%wx%h,%[exif:orientation]' '/var/folders/js/038lfs452n362y3xc7phyz100000gn/T/b2aa763204471d9d58ed3488ce58a26b20160713-54694-1129s9z.jpg[0]' 2>/dev/null[paperclip] An error was received while processing: #<Paperclip::Errors::NotIdentifiedByImageMagickError: Paperclip::Errors::NotIdentifiedByImageMagickError>   (0.1ms)  begin transactionCommand :: PATH=/Users/QJpioneer/ImageMagick-7.0.1/bin/:$PATH; file -b --mime '/var/folders/js/038lfs452n362y3xc7phyz100000gn/T/993316952cab27089c21a4f6fdd5299020160713-54694-1e7exev.jpg'   (0.1ms)  rollback transactionRedirected to http://localhost:3000/postsCompleted 302 Found in 143ms (ActiveRecord: 0.3ms)Started GET "/posts" for ::1 at 2016-07-13 07:15:08 +0800Processing by PostsController#index as HTML  Post Load (0.2ms)  SELECT "posts".* FROM "posts"  Rendered posts/index.html.erb within layouts/application (1.1ms)Completed 200 OK in 32ms (Views: 31.3ms | ActiveRecord: 0.2ms)