I want to simply save the token parameter I receive from the JSON request, but I simply cannot get to show if it is actually saved. I’ve observed that if you do a POST request with JSON parameters rails routes it to the create method. And I set my global variables attribute (in this case token), however when it redirects to the index.html.erb it gives me the error below
<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>
Here is my controller class;
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
And just in case here is the model I’ve generated (without using scaffold);
class Init < ActiveRecord::Base
attr_accessible :token
end
Here is my show.html.erb file;
<h1>Hello, World</h1>
<b>The token is:</b>
<%= @init.token%>
First, create does not redirect to index, it redirects to the new created @init path. Second, it looks like the show haml file contains a reference (in line 3) to @init.token, and since @init is nil you get this error.