I’m making a conventional forum in Rails to practice. I have a Topic model and a nested Post model. Topics can have many Posts.
Topics#Show has a list of @topic.posts and then a new Post form.
# Topics#Show
def show
@topic = Topic.find(params[:id])
@post = @topic.posts.new
end
Submitting a new post sends it to Posts#Create
# Posts#Create
def create
@topic = Topic.find(params[:topic_id])
@post = @topic.posts.new(params[:post])
@post.user = current_user
if @post.save
redirect_to @topic, :notice => "Successfully created post."
else
render :action => 'new' # <-- Unsure what to do here
end
end
If the Post fails to save, I want it to render Topics#Show and display the validation errors there.
From what I understand, params don’t persist through a redirect_to because a 302 redirect starts a new request.
You should render the topics/show view. So instead of
Do: