Comment forms and URLs

Hi all,

Just started with Rails a couple weeks ago after a few years doing PHP.
I've got a blog going with URLS like

/blog/2006/12/13/entry_title

and each entry has a comment form and the end. This form calls an
action 'add_comment' in the 'blog' controller, with the URL

/blog/add_comment

I was wondering whether it's possible for users to arrive back at the
first URL for the entry after trying to post a comment? Using
redirect_to with the entry's route means I can't use error_messages_for
on the comment in my entry's view. So instead, I'm just doing
@comment.save etc in blog#add_comment and then using

render :action show_entry

to show the entry the comment applies to. This retains the form values
and shows any errors produced. Can I have someone submit a comment and
be directed to

/blog/2006/12/13/entry_title

without losing the comment and its errors along the way?

Thanks,
James

James wrote:

Hi all,

[snip]

/blog/add_comment

I was wondering whether it's possible for users to arrive back at the
first URL for the entry after trying to post a comment? Using
redirect_to with the entry's route means I can't use error_messages_for
on the comment in my entry's view. So instead, I'm just doing
@comment.save etc in blog#add_comment and then using

render :action show_entry

to show the entry the comment applies to. This retains the form values
and shows any errors produced. Can I have someone submit a comment and
be directed to

/blog/2006/12/13/entry_title

without losing the comment and its errors along the way?

In the #add_comment action, store the comment in the user session before any
validation. Use flash to communicate error messages and redirect_to should work.

You can have a look at my blog comment section to get a better idea.

Long
www.edgesoft.ca/blog/read/2 - rails articles

Thanks for the suggestion! Just to clarify, would something like this
do the trick...

# in blog#add_comment
@comment = Comment.new(params[:comment])
@comment.valid?
flash[:comment] = @comment
redirect_to entry_url (...with parameters for URL...)

# in blog#show_entry
@comment = flash[:comment]

Will the show_entry action/view now get the contents of the comment and
@comment.errors? Does flash work or should I use session?

Thanks for the suggestion! Just to clarify, would something like this
do the trick...

# in blog#add_comment
@comment = Comment.new(params[:comment])

You might want to store the comment in the user session. e.g
session[:comment] = @comment

@comment.valid?

You would test for validity here and set flash to show any error message.
e.g. flash[:alert] = 'Missing a required field'

flash[:comment] = @comment
redirect_to entry_url (...with parameters for URL...)

# in blog#show_entry
@comment = flash[:comment]

You may use <% new_comment = session[:comment] %> and access each
attribute with
<%= new_comment.from %>
<%= new_comment.url %>
<%= new_comment.message%>
...etc. to display each attribute on the form.

A final note, you should initialize session[:comment] = Comment.new the first
time the form is accessed so you don't get a nil error.

HTH,

Long
www.edgesoft.ca/blog/read/2 - rails articles