Я следую руководству по началу работы с Rails 4.0.0, http://guides.rubyonrails.org/getting_started.html#updating-posts, и у него была та же проблема, что и у пользователя, задавшего этот вопрос, UrlGenerationError в разделе 5.12 руководства по Rails. Однако после исправления проблемы с post_path и удаления '}', который вызывал ошибку, теперь я получаю сообщение об ошибке 'id' неопределенного метода, когда пытаюсь отредактировать сообщение. Я искал в stackoverflow и google людей с похожими проблемами, но не смог найти решение. Любая помощь горячо приветствуется.
Ниже мой контроллер сообщений и мой файл 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 %>