Sinatra: NoMethodError

The whole source code is here

I think I have a logical error in a program thread that returns a NoMethodError

Firstly, a piece of code that causes an error.

<input id="#identity" type="number" name="journal[identity]" value="<%= @journal.identity unless @journal.identity.nil? %>" /> #Error Text NoMethodError at /profile undefined method `identity' for nil:NilClass file: journal_form.erb location: block in singleton class line: 2 

The code inside the input tag is the exact part of the code that is described in the error text.

My program stream is like this.

  • User is registered in
  • If authentication is successful, it will be redirected to the /profile page
  • According to their roles / privileges, they will see different content inside the main area in '/ profile'. Content is a database.

1 is normal. Users can log in and out without a problem. For the second step, the code looks like this:

 #profile.erb <% if session[:user].role == 1 %> <%= erb :assign_doctor %> <% elsif session[:user].role == 2 %> <%= erb :journal_form %> <% elsif session[:user].role == 3 %> <pre> Silence! </pre> <% elsif session[:user].role == 4 %> <%= erb :doctor_screen %> <% end %> 

file 'journal_form.erb' in the second condition.

 <input id="#identity" type="number" name="journal[identity]" value="<%= @journal.identity unless @journal.identity.nil? %>" /> .... # some other attributes like that. <% if session[:user].role == 1 %> <% if journal.viewed == false %> <input id="#assigned_doctor" type = "text" name="journal[assigned_doctor]" /> <% else %> <input id="#assigned_doctor" type = "text" name="journal[assigned_doctor]" value="<%= @journal.assigned_doctor unless @journal.assigned_doctor.nil? %>" /> <% end %> 

I also created CRUD resources for journal model entries (in another file). And, not inferior to the views of CRUD in the profile , they work fine.

Perhaps the problem is that profile does not know the context passed into it, so it responds like this. But I don’t know how to fix it.

I can add more code if you want.

In summation:

When @journal == nil why <% =@journal.identity unless @journal.identity.nil?%> Returns an undefined method 'identity' for nil:NilClass ?

Below are some useful resources:

in user.rb (contains 3 classes / models) in the same directory with main.rb.

 # 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 

Program structure

 β”œβ”€β”€ 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 

Check if the log is zero.

 get '/profile' do if !authorized? redirect to('/login') else puts "nihil" if @journal.nil? erb :profile end end 

server log

 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 

@journal is nothing.

+5
source share
3 answers

It seems to me that your code relies on the existence of the @journal field. (And whose magazine is it anyway?) This, of course, is not installed in main.rb You should get the required data in the get '/profile' block. Sort of:

 get '/profile' do if !authorized? redirect to('/login') else @journal = Journal.get(params[:id]) erb :profile end end 

There are various ways to do this. As a rule, it is good to avoid any general state between calls and instead get all the necessary data in each particular request. (The data can be reused, as in your JournalHelper module.) If accessing the database becomes the neck of a bottle, the best way forward is usually to insert a cache in front of the database or web server.

+1
source

If @journal is nil, @ journal.identity or @ journal.almost_any_function, it will throw an error. I think you are actually here:

 <% =@journal.identity unless @journal.nil?%> 

as stated in the comments. Alternatively, #try is defined in rails, you can do:

 <%= @journal.try(:identity) %> 

Both will return zero without raising NoMethod error in cases where the @journal journal itself is zero. If @journal is defined, it will return @ journal.identity

+1
source

As I said in the comments, the most obvious solution is to use <%= @journal.identity unless @journal.nil?%> , Because if @journal == nil , then @journal.nil? will return true, and the entire operator will return nil. I will continue to update this answer when you post the full code.

+1
source

All Articles