title: Sinatra: NoMethodError
body: <p><strong><a href="https://github.com/marxistcheguevara/mis" rel="nofollow">Whole source code here</a></strong></p>
  
  <p>I think I have a logic error in program flow, which returns NoMethodError </p>
  
  <p>First, a piece of code which causes error.</p>
  
  <pre><code>&lt;input id="#identity" type="number" name="journal[identity]" value="&lt;%= @journal.identity  unless @journal.identity.nil? %&gt;" /&gt;
  
  
  #Error Text
  NoMethodError at /profile
  undefined method `identity' for nil:NilClass
  file: journal_form.erb location: block in singleton class line: 2 
  </code></pre>
  
  <p>The code inside the input tag is the exact code piece which described in error text. </p>
  
  <p>My program flow is like that. </p>
  
  <ol>
  <li>User logs in</li>
  <li>If authentication is successful, he/she will be redirected to <code>/profile</code> page</li>
  <li>According to their roles/privileges they will see different content inside main area in '/profile'. Content is database</li>
  </ol>
  
  <p>1 is ok. Users can log in and out without problem. For 2nd step, code is like that</p>
  
  <pre><code>#profile.erb
  &lt;% if session[:user].role == 1 %&gt;
      &lt;%= erb :assign_doctor %&gt;
  &lt;% elsif session[:user].role == 2 %&gt;
      &lt;%= erb :journal_form %&gt;
  &lt;% elsif session[:user].role == 3 %&gt;
      &lt;pre&gt;
          Silence!        
      &lt;/pre&gt;
  &lt;% elsif session[:user].role == 4 %&gt;
      &lt;%= erb :doctor_screen %&gt;
  
  &lt;% end %&gt;
  </code></pre>
  
  <p>the 'journal_form.erb' file in second condition.</p>
  
  <pre><code>&lt;input id="#identity" type="number" name="journal[identity]" 
          value="&lt;%= @journal.identity  unless @journal.identity.nil? %&gt;" /&gt;
  
  .... # some other attributes like that.
  
  &lt;% if session[:user].role == 1 %&gt;
      &lt;% if journal.viewed == false %&gt;
  &lt;input id="#assigned_doctor" type = "text" name="journal[assigned_doctor]" /&gt;
      &lt;% else %&gt;
  &lt;input id="#assigned_doctor" type = "text" name="journal[assigned_doctor]" value="&lt;%= @journal.assigned_doctor  unless @journal.assigned_doctor.nil? %&gt;" /&gt;
      &lt;% end %&gt;
  </code></pre>
  
  <p>I've also created CRUD resources for <code>journal</code> model entries (in other file). And without yielding CRUD views into <code>profile</code> they work ok. </p>
  
  <p>Maybe problem is, the <code>profile</code> is not aware of the context passed into it, so it responses like that. But have no any idea how to fix it. </p>
  
  <p>I can add more code, if you want.</p>
  
  <p><strong>In summation:</strong></p>
  
  <p>When <code>@journal == nil</code> why does <code>&lt;%=@journal.identity  unless @journal.identity.nil?%&gt;</code> return <code>undefined method 'identity' for nil:NilClass</code>?</p>
  
  <p>Below there are some helpful resources:</p>
  
  <p>in <strong>user.rb</strong> (contains 3 classes/models) in the same directory with main.rb. </p>
  
  <pre><code># model declerations end here
  DataMapper.finalize
  
  module JournalHelpers
      def find_journals
          @journals = Journal.all
      end
  
      def find_journal
          Journal.get(params[:id])
      end
  
      def create_journal
          @journal = Journal.create(params[:journal])
      end
  end
  
  helpers JournalHelpers
  
  get '/journals' do 
      find_journals
      erb :journals
  end
  
  #new
  get '/journals/new' do
      #protected!
      @journal = Journal.new
      erb :new_journal
  end
  
  
  #show
  get '/journals/:id' do
      @journal = find_journal
      erb :show_journal
  end
  
  #create
  post '/journals' do
      #protected!
    flash[:notice]= "Journal was succesfull posted" if create_journal
    redirect to("/journals/#{@journal.id}")
  end
  
  #edit
  get '/journals/:id/edit' do
      #protected!
      @journal = find_journal
      erb :edit_journal
  end
  
  #put/update
  
  put '/journals/:id' do
      #protected!
      journal = find_journal
      if journal.update(params[:journal])
          flash[:notice] = "Journal successfully updated"
      end
      redirect to("/journals/#{journal.id}")
  end
  </code></pre>
  
  <p><strong>Program structure</strong></p>
  
  <pre><code>├── assets
  │   ├── css
  │   │   ├── application.css
  │   │   ├── jquery-ui.min.css
  │   │   └── main.css
  │   ├── images
  │   │   ├── loader.gif
  │   │   └── nurse_shshsh.jpeg
  │   └── js
  │       ├── application.js
  │       ├── jquery.min.js
  │       └── jquery-ui.min.js
  ├── main.rb
  ├── user.rb
  ├── users.db
  └── views
      ├── about.erb
      ├── assign_doctor.erb
      ├── contact.erb
      ├── doctor_screen.erb
      ├── edit_journal.erb
      ├── home.erb
      ├── journal_form.erb
      ├── journals.erb
      ├── layout.erb
      ├── leftcolumn.erb
      ├── login.erb
      ├── nav.erb
      ├── new_journal.erb
      ├── notifications.erb
      ├── profile.erb
      └── show_journal.erb
  </code></pre>
  
  <p><strong>Checking for if journal is nil.</strong> </p>
  
  <pre><code>get '/profile' do 
      if !authorized?
          redirect to('/login')
      else
          puts "nihil" if @journal.nil?
          erb :profile
      end
  end
  </code></pre>
  
  <p>server log</p>
  
  <pre><code>127.0.0.1 - - [29/Jun/2015:22:35:33 +0500] "GET /profile HTTP/1.1" 302 - 0.0029
  127.0.0.1 - - [29/Jun/2015:22:35:33 +0500] "GET /login HTTP/1.1" 200 212 0.0024
  127.0.0.1 - - [29/Jun/2015:22:35:42 +0500] "POST /login HTTP/1.1" 303 - 0.0167
  nihil
  127.0.0.1 - - [29/Jun/2015:22:35:43 +0500] "GET /profile HTTP/1.1" 200 1047 0.0106
  </code></pre>
  
  <p>@journal is nil.</p>