Ruby   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ruby – Sinatra:NoMethodError大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
Whole source code here

我想程序流中有一个逻辑错误,返回NoMethodError

首先,一段导致错误的代码.

<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

输入标记内的代码是错误文本中描述的确切代码段.

我的程序流程就是这样.

>用户登录@H_696_13@>如果身份验证成功,他/她将被重定向到/ profile页面@H_696_13@>根据他们的角色/特权,他们将在’/ profile’的主区域内看到不同的内容.内容是数据库

1还可以.用户可以毫无问题地登录和注销.对于第二步,代码就是这样

#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 %>

第二个条件下的’journal_form.erb’文件.

<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 %>

我还为期刊模型条目创建了CRUD资源(在其他文件中).并且不将CRUD视图放入配置文件中,它们可以正常工作.

也许问题是,配置文件不知道传入其中的上下文,因此它会像这样响应.但不知道如何解决它.

如果你愿意,我可以添加更多代码.

总结:

当@journal == nil为什么<%= @ journal.identity除非@ journal.identity.nil?%>为nil返回undefined方法’identity’:NilClass?

下面是一些有用的资源:

在user.rb(包含3个类/模型)与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

计划结构

├── 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

检查期刊是否为零.

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

服务器日志

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是零.

解决方法

在我看来,您的代码依赖于@journal字段集的事实. (无论如何,它的期刊是什么?)肯定没有在main.rb中设置.您应该在get’/ profile’块中获取所需的数据.有点像:
get '/profile' do
  if !authorized?
    redirect to('/login')
  else
    @journal = Journal.get(params[:id])
    erb :profile
  end
end

有多种方法可以做到这一点.通常最好避免调用之间的任何共享状态,而是在每个特定请求中获取所有需要的数据. (数据仍然可以像在JournalHelper模块中那样重复使用.)如果数据库访问成为瓶颈,最好的方法通常是在数据库或Web服务器前引入缓存.

大佬总结

以上是大佬教程为你收集整理的ruby – Sinatra:NoMethodError全部内容,希望文章能够帮你解决ruby – Sinatra:NoMethodError所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。