JSONリクエストから受け取ったパラメーターを単純に保存したいのですがtoken
、実際に保存されているかどうかを表示できません。JSONパラメーターを使用してPOSTリクエストを実行すると、Railsがそれをcreate
メソッドにルーティングすることを確認しました。そして、グローバル変数属性(この場合はtoken )を設定しましたが、 index.html.erbにリダイレクトすると、以下のエラーが発生します。
<h1>
NoMethodError in
Inits#show
</h1>
<p>
Showing <i>/Users/alioral/Desktop/Rails/testApp2/app/views/inits/show.html.erb</i> where line <b>#3</b> raised:
<pre><code>undefined method `token' for nil:NilClass</code></pre>
</p>
これが私のコントローラークラスです。
class InitsController < ApplicationController
def index
end
def show
end
def create
@init=Init.new("token"=>params[:token])
@init.save
respond_to do |format|
format.html { redirect_to @init }
format.json { render json: @init }
end
end
end
そして念のため、ここに私が生成したモデルがあります(足場を使用せずに)。
class Init < ActiveRecord::Base
attr_accessible :token
end
これが私のshow.html.erbファイルです。
<h1>Hello, World</h1>
<b>The token is:</b>
<%= @init.token%>