使用 Rails 和 HAML 时出现错误:未定义的方法“名称”;对于 nil:NilClass
我是 Rails 的新手,并且在 Action 3 中基于 Rails 设置了 Ticketee 项目,我尝试稍微偏离一下并使用 views/projects/show.html.haml 设置 haml
我确实配置了 Gemfile 并安装了 haml,并且我测试了一个基本的 haml 页面,它确实有效。
这是我的问题,当将 show.html.erb 迁移到 haml 时,我收到一个无法解决的错误。
显示.html.erb:
<h1><%= @project.name %></h1>
如果我转到 http://localhost:3000/projects/1 它显示项目名称,则显示正常id 为 1。show.html.haml
:
%h2= @project.name
当我用 haml 替换 show.html.erb 并转到上面的 url 后,我得到:
项目中没有方法错误#show
显示 /ticketee/app/views/projects/show.html.haml 第 1 行提出的位置:
nil 的未定义方法“名称”:NilClass 提取的源代码(围绕行 #1):
1: %h2= @project.name Rails.root: /持票人
应用程序跟踪|框架跟踪 |完整追踪 应用程序/视图/项目/show.html.haml:1:in `_app_views_projects_show_html_haml___2329513113615295829_70311891362660'
schema.rb 肯定有名称字段:
ActiveRecord::Schema.define(:version => 20120212051007) do
create_table "projects", :force => true do |t|
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
end
我的控制器:
class ProjectsController < ApplicationController
def index
end
def new
@project = Project.new
end
def create
@project = Project.new(params[:project])
@project.save
#flash[:notice] = "Project has been created."
redirect_to @project, :notice => "Project has been created."
end
end
我认为这只是我的一些疏忽,因为这是 haml 的一个非常基本的用法。
I'm new to rails and setting up the ticketee project based on Rails in Action 3, I'm attempting to deviate slightly and setup haml with views/projects/show.html.haml
I do have the Gemfile configured and haml installed and I tested a basic haml page and it does work.
Here is my problem, when migrating the show.html.erb to haml I am getting an error I can't resolve.
show.html.erb:
<h1><%= @project.name %></h1>
This displays fine if I go to http://localhost:3000/projects/1 it displays the project name with id 1.
show.html.haml:
%h2= @project.name
After I replace show.html.erb with the haml and I go to the above url I get:
NoMethodError in Projects#show
Showing /ticketee/app/views/projects/show.html.haml
where line #1 raised:undefined method `name' for nil:NilClass Extracted source (around line
#1):1: %h2= @project.name Rails.root:
/ticketeeApplication Trace | Framework Trace | Full Trace
app/views/projects/show.html.haml:1:in
`_app_views_projects_show_html_haml___2329513113615295829_70311891362660'
The schema.rb definitely has the name field:
ActiveRecord::Schema.define(:version => 20120212051007) do
create_table "projects", :force => true do |t|
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
end
And my controller:
class ProjectsController < ApplicationController
def index
end
def new
@project = Project.new
end
def create
@project = Project.new(params[:project])
@project.save
#flash[:notice] = "Project has been created."
redirect_to @project, :notice => "Project has been created."
end
end
I assume this is just some oversight on my part as this is a very basic usage of haml.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的项目控制器中似乎没有 show 方法。由于
projects/1
意味着它是一个特定的记录,因此将调用显示页面。当/projects
被调用时,index方法将被调用。一个可能的显示方法可以是:
在此方法中,将实例变量分配给
@project
,其 ID 位于 URL 中。这可能是您问题的答案!It seems that you don't have a show method inside of your Project controller. Since
projects/1
means that it is a particular record, the show page would be called. When/projects
is called, the index method would get called.A possible show method could be:
In this method, an instance variable is assigned to
@project
with the ID which is in the URL. This could be the answer to your problem!