環境
- Ruby 2.1.1
- RubyOnRails 4.1.2
- Mac OS X 10.9.3
json.extract!
json.extract! @sample, :id, :text
class SampleController < ApplicationController
def test
@sample = Sample.find(params[:id])
respond_to do |format|
format.json
end
end
end
Rails.application.routes.draw do
get "/sample/:id" => "sample#test
end
〜〜〜〜
gem 'jbuilder', '~> 2.1.1'
〜〜〜〜
という形で、json形式で返そうとしています。
なお、jbuilderはインストール済みでGemfileにも記述されています。
この時のエラー
GET → localhost:3000/sample/1.jbuilder.json
Routing Error
No route matches [GET] "/sample/1.jbuilder.json"
ためしたこと
- パスが通っているか
- bundle install
調査
class SampleController < ApplicationController
def test
@sample = Sample.find(params[:id])
respond_to do |format|
format.json { render json: @sample }
end
end
end
GET → localhost:3000/sample/1.json
{1, 〜〜〜〜}
あたりまえですが、JSONをそのまま返すと表示は問題無いです。
JSONデータを貰いたかっただけなのですが、なぜかjbuilderのやり方はうまく行かず(何かがおかしいのですが、わかりませんでした)、結局微妙な感じになりました。
解決策
あまりにしょぼすぎて紹介するのも微妙だが、もし同じところでつまづいている方がいたら参考にしてくださいw
json.extract! @sample, :id, :text
GET → localhost:3000/sample/1.json
具体的に言いますと以下の2点を修正したら治りました。
- 拡張子がおかしかった
- GETリクエストがおかしかった
nokogiri の bundle install エラー
Building nokogiri using packaged libraries.
Building libxml2-2.8.0 for nokogiri with the following patches applied:
- 0001-Fix-parser-local-buffers-size-problems.patch
- 0002-Fix-entities-local-buffers-size-problems.patch
- 0003-Fix-an-error-in-previous-commit.patch
- 0004-Fix-potential-out-of-bound-access.patch
- 0005-Detect-excessive-entities-expansion-upon-replacement.patch
- 0006-Do-not-fetch-external-parsed-entities.patch
- 0007-Enforce-XML_PARSER_EOF-state-handling-through-the-pa.patch
- 0008-Improve-handling-of-xmlStopParser.patch
- 0009-Fix-a-couple-of-return-without-value.patch
- 0010-Keep-non-significant-blanks-node-in-HTML-parser.patch
- 0011-Do-not-fetch-external-parameter-entities.patch
************************************************************************
IMPORTANT! Nokogiri builds and uses a packaged version of libxml2.
If this is a concern for you and you want to use the system library
instead, abort this installation process and reinstall nokogiri as
follows:
gem install nokogiri -- --use-system-libraries
If you are using Bundler, tell it to use the option:
bundle config build.nokogiri --use-system-libraries
bundle install
However, note that nokogiri does not necessarily support all versions
of libxml2.
解決策
なんだかよくわからないが、
$ bundle config build.nokogiri --use-system-libraries
If you are using Bundlerだったので、上のコマンドを叩くとbundle install うまくいった
(未解決)ActiveRecord ActiveResource
なぜかActiveResourceを使うとエラーになる
解決策
要検証
rails g scaffold / modelが動かない
解決策
Gemfileに以下のプラグインを追加したら動かなくなった。なので消せば動きます。
検証が必要
- gem 'pry-doc'
- gem 'pry-stack_explorer'
modelでmethod作るとき
def test(file)
file.read do |row|
test = Test.new
test.date = row[0]
test.name = row[1]
test.flg = false
test.save
end
end
これだとうまくいかないので、
def self.test(file)
file.read do |row|
test = Test.new
test.date = row[0]
test.name = row[1]
test.flg = false
test.save
end
end
こちらに変更したらうまくいった。
unicornの起動時エラー
/usr/local/lib/ruby/2.1.0/fileutils.rb:250:in `mkdir': Permission denied @ dir_s_mkdir - tmp (Errno::EACCES)
from /usr/local/lib/ruby/2.1.0/fileutils.rb:250:in `fu_mkdir'
from /usr/local/lib/ruby/2.1.0/fileutils.rb:224:in `block (2 levels) in mkdir_p'
from /usr/local/lib/ruby/2.1.0/fileutils.rb:222:in `reverse_each'
from /usr/local/lib/ruby/2.1.0/fileutils.rb:222:in `block in mkdir_p'
from /usr/local/lib/ruby/2.1.0/fileutils.rb:208:in `each'
from /usr/local/lib/ruby/2.1.0/fileutils.rb:208:in `mkdir_p'
from /usr/local/lib/ruby/gems/2.1.0/gems/unicorn-4.8.3/bin/unicorn_rails:202:in `block in <top (required)>'
from /usr/local/lib/ruby/gems/2.1.0/gems/unicorn-4.8.3/lib/unicorn/configurator.rb:83:in `call'
from /usr/local/lib/ruby/gems/2.1.0/gems/unicorn-4.8.3/lib/unicorn/configurator.rb:83:in `reload'
from /usr/local/lib/ruby/gems/2.1.0/gems/unicorn-4.8.3/lib/unicorn/configurator.rb:68:in `initialize'
from /usr/local/lib/ruby/gems/2.1.0/gems/unicorn-4.8.3/lib/unicorn/http_server.rb:100:in `new'
from /usr/local/lib/ruby/gems/2.1.0/gems/unicorn-4.8.3/lib/unicorn/http_server.rb:100:in `initialize'
from /usr/local/lib/ruby/gems/2.1.0/gems/unicorn-4.8.3/bin/unicorn_rails:209:in `new'
from /usr/local/lib/ruby/gems/2.1.0/gems/unicorn-4.8.3/bin/unicorn_rails:209:in `<top (required)>'
from /usr/local/bin/unicorn_rails:23:in `load'
from /usr/local/bin/unicorn_rails:23:in `<main>'
master failed to start, check stderr log for details
ソースを見ます
250 Dir.mkdir path
解決策
何か、ファイルを作ろうとしてこけてますので、tmpを作ろうとしている直前にputs仕込んで、そこにchmodで権限追加するか、グループをroot以外のユーザにも付与すればおk
※unicorn系のエラーはGitHubの方に対応内容をめもってある。
JavaScript runtimeエラー
`autodetect': Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)
解決策Gemfileに以下追加
gem 'therubyracer', platforms: :ruby
環境
- Ruby 2.2.1
- RubyOnRails 4.2.1
- Mac OS X 10.10.2
bundle exec rails sできない
warning: already initialized constant APP_PATH
solve
bundle update