Refill a form when invalid data is submitted

I am trying to create blogging software using Ruby on Rails. I have a
CommentsController class and an EntriesController class. The file
app/views/entries/show.html.erb contains the code "<%= render
'comments/form' %>", which displays a form on the page for an individual
entry to add a comment on that entry. When a user submits a comment but
does something invalid like submit a blank comment or a blank name I
want the user to be redirected back to the entry's page with the comment
filled out with whatever data was filled in. I tried the following code
inside my "create" function in the comments_controller.rb file:

    respond_to do |format|
      if !(@comment.user_id.blank? && (@comment.guest_name.blank? ||
@comment.guest_email.blank?)) && @comment.save
        format.html { redirect_to Entry.find(@comment.entry_id), notice:
'Comment was successfully created.' }
        format.json { render json: @comment, status: :created, location:
@comment }
      else
        if @comment.nil?
          format.html { redirect_to "/entries" }
        else
          format.html { render :template => "/entries/show.html" }
        end
        format.json { render json: @comment.errors, status:
:unprocessable_entity }
      end
    end

I realize that the line 'format.html { render :template =>
"/entries/show.html" }' is incorrect. One problem is that the URL does
not contain the entry's ID, but when I replaced "entries.show.html" with
'"/entries/"+@comment.entry_id.to_s' it says "Missing template entries/1
with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder,
:coffee]}." When I replace the "render :template => " with
"redirect_to", I get redirected back to the page that I want to but the
form data is not filled out with whatever the user filled in.

I suggest that, as a beginner, you work right through a good tutorial
such as railstutorial.org, which is free to use online. That will
show you the basics of Rails, including how to solve your problem.

Colin

I suggest that, as a beginner, you work right through a good tutorial
such as railstutorial.org, which is free to use online. That will
show you the basics of Rails, including how to solve your problem.

Thank you for the suggestion, but I already went through that tutorial a
few days ago. I looked at the files of the sample app I created for that
tutorial, and in the users' controller there is the code "render 'new'"
in the "create" function which the program only processes when
@user.save fails. In that app, when a form is submitted it is returned
with the invalid data already filled out for the user to correct.
However, this does not work on the blogging app that I am trying to
create.

There are two things that I believe make my problem different from the
situation in the tutorial. First, I am trying to get the comments
controller to render a page in the entry's view while in the tutorial
the user's controller is accessing a page in the user's view. Another
difference is that when the app for the tutorial is accessing a view it
has no parameter for that view, while in my app I am trying to access
'app/views/entries/show.html.erb' with a parameter. I want to access the
URL '/entries/1' with the form filled out.

I don't know why people are generally so unhelpful on these message
boards. I've been a professional programmer for years, and I've seen so
many people turned away because people just say "go read an entire book
for this one simple problem" rather than helping new users along.

Anyway, this question is very old, but here is a solution for anyone
that wants to learn.

Instead of using 'redirect_to' on invalid data, you can use 'render'.

Here is an example with a blog.

    @blog= Blog.create(blog_params)
    if @blog.valid?
      redirect_to workshops_path
    else
      render '/blog/new' # or whatever view you're using
    end

That's it. For the next person, no need to read an entire tutorial.
Hopefully this helps someone new build something great.

Because so often when a user asks a simple question and it is answered
with a simple answer then a few hours later or the next day the user
asks another simple question, and another and another. Therefore I
always suggest that beginners take a few days to work right through a
good tutorial so that they understand the basics of rails, and then
get back to trying to solve their particular problem. In this
particular case it appears the guy says 'I already went through the
tutorial a few days ago'. It is not clear to me whether that meant he
skipped through it or worked right through, doing the exercises and so
on. Whichever, that does not invalidate my initial suggestion, as I
had no information as to whether he had already worked through a
tutorial or not.

If a beginner is put off learning a craft by someone suggesting that
he spends some time educating himself then one has to wonder about his
commitment in the first place.

I think if you look through the archives you will see that many times
I have helped users along as you put it, but I feel it is also
incumbent on them to put some effort in to help themselves.

Colin

Such an excellent answer !

please allow me to elaborate on Colin’s reply:

Colin is - if you trace the archives - one of this maillists most helpful and you will have to look hard to find him ‘sending off people’ that infact to put in the hours.

It is a trend in society that you ‘show off’ from the get go, only to realize that it really does require some hard work to do anything and the old saying: do something for 10,000 hours and then you will master it, still hold!

We all benefit from open source and the astronomical hours put into open source projects like Rails by a legion of programmers - and we get to use it for free ! All we have to do is our effort and interest - and demonstrate that when we ask for guidance and problem solving.

I believe that is a fair request

Walther