views:

44

answers:

2

Hi all,

Using the perennial example of a Blog webapp (and all resources are currently automatically mapped in routes.rb):

I have a simple index page that lists all my Post titles. I would like to add a form at the bottom of this page to quickly create new Posts.

I'm new to Rails and can't seem to figure this out! Please help!

A: 

Try something like:

<% form_for Post.new do |form| %>

(Insert fields here:)    

<%= form.label :fieldname %>
<%= form.text_field :fieldname %>

<%= form.submit "Create" %>

<% end %>

Add this to app/views/posts/index.html.erb.

Veeti
Following up on this: so the Home controller has 2 actions: 'index' and 'create'. The 'index' action lists some Posts and has a form at the bottom to create a new Post. When you create a post here, it doesn't seem to use the 'create' action in the Home controller, but rather the one in Post controller? How can I make it use the 'create' action in the Home controller if there's where the post was created? I'm guessing something needs to be changed in routes.rb?
cravr
You could create a route specific for the form in question, and do something like `form_for Post.new, :url => { :controller => "home", :action => "create" }`.
Veeti
A: 

Yes you have both index and create actions in the index view.

display list in a index.html.erb then form to create a new post.

after creating new post redirect to index action only.

Salil