Understanding Form_for

OK, so I'm an absolute noob at Ruby on Rails. I've read a lot of
information I can find about form_for and most of them use form_for to
create new items that get saved into the database and can be viewed
later to edit, destroy, etc. In this case, the database will fill up
with whatever the user wrote and I just want my program to have no
information stored.

I've been trying to take a user's input from the text_field or
file_field and once they hit the "submit" button, they get redirected
to another page that, for instance, says "You wrote: [insert user
input here]" I've been having no luck whatsoever doing this except to
use HTML form, but I was hoping I can do this in Ruby on Rails.

Thanks for any help,
anon_comp

If the form is not associated with a model then form_tag may be more
appropriate.

Colin

Going by your suggestion, Colin, my project looks like this

trying_controller.rb:
class TryingController < ApplicationController
  def index
  end

  def to
    @name = params[:title]
  end
end

index.html.erb:
<% form_tag :action => :to %>
<label for="name">Name:</label>
<%= text_field_tag :name, params[:name] %>
<%= submit_tag "OK" %>
</form>

to.html.erb:
Name: <%= @name %>

The submit button doesn't work...

In what way does it not work?

Did you check the html to see if it is what you expect?

I think it should be :action = 'to' rather than :to

Also when things are not working as you expect the log file
(log/development.log) can often be useful in analysing the problem.

Colin

In what way does it not work?

When I hit the "OK" button nothing happens, it stays on the same page.

Did you check the html to see if it is what you expect?

I ran the server and looked at the page if that's what you mean, yes.

I meant that you should check that the html for the form is correct
html for a form with a submit button. I think you may find that there
is a </form> where you do not expect it, (separate from the one you
have manually added).

Looking at your code you have got the use of form_tag wrong.
Unfortunately you have snipped your original code so it is difficult
to insert the corrections. Have another look at the docs for
form_tag, you will see there should be a 'do' there and there is no
need to add the </form> yourself.

I think it should be :action = 'to' rather than :to

It didn't affect anything, I beleive they mean the same thing.

They don't mean the same thing, though sometimes they are
interchangeable. I am not sure in this case, again check the html to
see if it is correct.

Colin

In what way does it not work?

When I hit the "OK" button nothing happens, it stays on the same page.

Did you check the html to see if it is what you expect?

I ran the server and looked at the page if that's what you mean, yes.

I meant that you should check that the html for the form is correct
html for a form with a submit button. I think you may find that there
is a </form> where you do not expect it, (separate from the one you
have manually added).

Use View, Page Source, or similar, in your browser to view the html.

Colin

Problem solved.

Thank you very much Colin for the excellent help :slight_smile:

I just had to change the def to in my controller

class TryingController < ApplicationController
  def index
  end

  def to
    @name = params[:name]
  end
end