Reputation: 1149
In my forum, reply_to and quoted variables are passed into the new post page, enabling replies and quotes very nicely. The problem comes in when the user does something wrong; for instance, if the post is too short, the controller renders 'posts/new' with a flash error.
I cannot for the life of me get the controller to pass these upon rendering. Here's my setup.
Both variables are initialized in the new method:
def new
@post = Post.new
init_quoted
init_reply_to
if @quoted
@post.content = "[quote="[email protected]+"]"[email protected]+"[/quote]"
end
end
def init_reply_to
if params[:reply_to]
@reply_to = Discussion.find(params[:reply_to])
end
end
def init_quoted
if params[:quoted]
@quoted = Post.find(params[@quote])
end
end
This works great when the user makes no mistakes. From the "else" onward in the following code, however, variables aren't getting passed:
def create
@post = current_user.posts.build(params[:post])
if @post.save
flash[:success] = "You reply has been added."
redirect_to controller: 'discussions', action: 'show', id: @post.discussion.id, anchor: 'post'[email protected]_s
else
render template: 'posts/new', locals: { reply_to: @reply_to, quoted: @quoted }
end
end
Am I missing something? The variables should be global, so why aren't they being transferred?
Upvotes: 0
Views: 220
Reputation: 10395
Well you don't call your init functions in create
This should work :
def create
@post = current_user.posts.build(params[:post])
if @post.save
flash[:success] = "You reply has been added."
redirect_to controller: 'discussions', action: 'show', id: @post.discussion.id, anchor: 'post'[email protected]_s
else
init_quoted
init_reply_to
render template: 'posts/new'
end
end
No need to specify them as locals, just access them as @quoted
and @reply_to
in the view
Upvotes: 1