我按照Rails 4.0.0,http://guides.rubyonrails.org/getting_started.html#updating-posts的入門教程,遇到與提問這個問題的用戶相同的問題,UrlGenerationError on section 5.12 of Rails Guide。但是,在解決了他使用post_path的問題並刪除了引發錯誤的'}'之後,當我嘗試編輯帖子時,現在我收到了未定義的方法'id'錯誤消息。我一直在尋找stackoverflow和谷歌的人有類似的問題,但一直沒能找到解決方案。任何幫助深表感謝。未定義的方法'id' - 第5.12節更新文章
下面是我的帖子控制器和我edit.html.erb文件
posts_controller.rb:
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
edit.html.erb:
<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 %>
你能指出這個錯誤指向哪一行嗎? – dax