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.