# [Ruby On Rails]使用hash_dot gem用dot來呼叫hash的屬性 ###### tags: `Ruby On Rails` url: https://github.com/adsteel/hash_dot 安裝gem: ```terminal bundle add hash_dot bundle install ``` 使用方式: controller ```ruby class HomeController < ApplicationController #require 'hash_dot' <--這句不需要, #上面這句不需要,因為rails預設會引用所有Gemfile中的gem def index @user={ name:"user1", }.to_dot #<--若只希望個別的hash支援hash_dot則在Hash結尾加此語 end end ``` 或者在該controller中直接允許所有hash開啟hash_dot支援 ```ruby class HomeController < ApplicationController require 'hash_dot' Hash.use_dot_syntax = true #允許所有hash開啟hash_dot支援 def index @user={ name:"user1", } end end ``` 若要在整個專案中使用hash_dot: config/application.rb ```ruby #... Hash.use_dot_syntax = true #<--加上這句 ``` view ```rust <%=@user.name%> ``` >記得要重啟server
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up