我正在研究RailsGuides教程(创建一个博客应用程序).当我运行服务器并打开时:/posts/new一切看起来都很好.但是,当我尝试创建帖子时,我收到此错误:
帖子#show中的NoMethodError
显示/home/darek/rails_projects/blog/app/views/posts/show.html.erb在行#3提出:
nil的未定义方法`title':NilClass
提取的来源(第3行):
1 <p>
2 <strong>Title:</strong>
3 <%= @post.title %>
4 </p>
5 <p>
Run Code Online (Sandbox Code Playgroud)
事实上post已创建,我可以在/ posts看到标题和内容但是当我尝试使用show特定帖子时,我收到此错误.我的第一个线索就是换线
<%= @post.title %>
Run Code Online (Sandbox Code Playgroud)
至
<%= @post.try(:title) %>
Run Code Online (Sandbox Code Playgroud)
错误消失了,但问题没有解决.
当我尝试显示特定帖子时,我获得了标题,而文本形式为空.这不是我想要看到的;)
好的,这是代码
Show.html.erb
<p>
<strong>Title:</strong>
<%= @post.title %>
</p>
<p>
<strong>Text:</strong>
<%= @post.text %>
</p>
<h2>Add a comment:</h2>
<%= form_for([@post, @post.comments.build]) do |f| %>
<p>
<%= f.label :commenter %><br />
<%= f.text_field :commenter %>
</p>
<p>
<%= f.label :body %><br />
<%= f.text_area :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<%= link_to 'Edit Post', edit_post_path(@post) %> |
<%= link_to 'Back to Posts', posts_path %>
Run Code Online (Sandbox Code Playgroud)
Posts_controller.rb
class PostsController < ApplicationController
def new
@post = Post.new
end
def index
@posts = Post.all
end
def create
@post = Post.new(params[:post].permit(:title, :text))
if @post.save
redirect_to @post
else
render 'new'
end
end
private
def post_params
params.require(:post).permit(:title, :text)
end
def show
@post = Post.find(params[:id])
end
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
if @post.update(params[:post].permit(:title, :text))
redirect_to @post
else
render 'edit'
end
end
def destroy
@post = Post.find(params[:id])
@post.destroy
redirect_to posts_path
end
end
Run Code Online (Sandbox Code Playgroud)
耙路:
-VirtualBox:~/rails_projects/blog$ rake routes
Prefix Verb URI Pattern Controller#Action
post_comments GET /posts/:post_id/comments(.:format) comments#index
POST /posts/:post_id/comments(.:format) comments#create
new_post_comment GET /posts/:post_id/comments/new(.:format) comments#new
edit_post_comment GET /posts/:post_id/comments/:id/edit(.:format) comments#edit
post_comment GET /posts/:post_id/comments/:id(.:format) comments#show
PATCH /posts/:post_id/comments/:id(.:format) comments#update
PUT /posts/:post_id/comments/:id(.:format) comments#update
DELETE /posts/:post_id/comments/:id(.:format) comments#destroy
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
root GET / welcome#index
GET /posts/:id(.:format) posts#view
DELETE /posts/:id(.:format) posts#destroy
Run Code Online (Sandbox Code Playgroud)
感谢您的帮助和兴趣!
Rai*_*Guy 59
你已经使你的方法变得私密.记住你把private关键字放在哪里.下面的所有方法将变为私有,定义你的方法.控制器末尾的私有方法:
class PostsController < ApplicationController
def new
@post = Post.new
end
def index
@posts = Post.all
end
def create
@post = Post.new(params[:post].permit(:title, :text))
if @post.save
redirect_to @post
else
render 'new'
end
end
def show
@post = Post.find(params[:id])
end
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
if @post.update(params[:post].permit(:title, :text))
redirect_to @post
else
render 'edit'
end
end
def destroy
@post = Post.find(params[:id])
@post.destroy
redirect_to posts_path
end
private
def post_params
params.require(:post).permit(:title, :text)
end
end
Run Code Online (Sandbox Code Playgroud)
希望它会有所帮助.谢谢
在学习本教程时,我遇到了同样的问题.我再次检查了我的代码然后找到了原因.在posts_controller.rb文件中,您不能将私有方法放在代码中间,这意味着下面的所有方法(例如show,edit)都将是私有的.相反,将私有方法放在底部,如下所示:
class PostsController < ApplicationController
def new
end
def index
@posts = Post.all
end
def create
@post = Post.new(post_params)
@post.save
redirect_to @post
end
def show
@post = Post.find(params[:id])
end
private
def post_params
params.require(:post).permit(:title, :text)
end
end
Run Code Online (Sandbox Code Playgroud)
希望能解决你的问题.
| 归档时间: |
|
| 查看次数: |
28318 次 |
| 最近记录: |