Ruby on rails 未定义的方法';id';-第5.12节更新员额
我正在学习Rails 4.0.0的入门教程,遇到的问题与提出此问题的用户相同。但是,在修复了他关于post_path的问题并删除了引发错误的“}”之后,我现在在尝试编辑post时收到了一条未定义的方法“id”错误消息。我一直在搜索stackoverflow和google,寻找一个有类似问题的人,但没有找到解决方案。非常感谢您的帮助 下面是我的Posts控制器和edit.html.erb文件 posts\u controller.rb:Ruby on rails 未定义的方法';id';-第5.12节更新员额,ruby-on-rails,ruby-on-rails-4,railstutorial.org,Ruby On Rails,Ruby On Rails 4,Railstutorial.org,我正在学习Rails 4.0.0的入门教程,遇到的问题与提出此问题的用户相同。但是,在修复了他关于post_path的问题并删除了引发错误的“}”之后,我现在在尝试编辑post时收到了一条未定义的方法“id”错误消息。我一直在搜索stackoverflow和google,寻找一个有类似问题的人,但没有找到解决方案。非常感谢您的帮助 下面是我的Posts控制器和edit.html.erb文件 posts\u controller.rb: class PostsController < App
class PostsController < ApplicationController
def new
@post = Post.new
end
def create
@post = Post.new(post_params)
if @post.save
redirect_to action: :show, id: @post.id
else
render 'new'
end
end
def show
@post = Post.find(params[:id])
end
def index
@posts = Post.all
end
private
def post_params
params.require(:post).permit(:title, :text)
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
end
<h1>Editing post</h1>
<%= form_for :post, url: post_path(@post.id) ,
method: :patch do |f| %>
<% if @post.errors.any? %>
<div id="errorExplanation">
<h2><%= pluralize(@post.errors.count, "error") %> prohibited
this post from being saved:</h2>
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<%= link_to 'Back', posts_path %>
class PostsController
edit.html.erb:
class PostsController < ApplicationController
def new
@post = Post.new
end
def create
@post = Post.new(post_params)
if @post.save
redirect_to action: :show, id: @post.id
else
render 'new'
end
end
def show
@post = Post.find(params[:id])
end
def index
@posts = Post.all
end
private
def post_params
params.require(:post).permit(:title, :text)
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
end
<h1>Editing post</h1>
<%= form_for :post, url: post_path(@post.id) ,
method: :patch do |f| %>
<% if @post.errors.any? %>
<div id="errorExplanation">
<h2><%= pluralize(@post.errors.count, "error") %> prohibited
this post from being saved:</h2>
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<%= link_to 'Back', posts_path %>
编辑文章
禁止
无法保存此帖子:
首先将您的编辑和更新方法移出私有范围!然后指向出现错误的那条线。你能指出这个错误指向的那条线吗?我觉得自己很笨。没有意识到“私人”是这样工作的。谢谢