post data help

I have a controller called mystuff_controller.rb, a db called mpstuff and a table inside that db called data and a column called m1. I was hoping that some could give a quick step by step on how to create a post action so that I can write input from my index.html.erb form to that column.

Hi Mark,

bill walton wrote: > Hi Mark, > >> I was hoping that some could give a quick step by step on how >> to create a post action so that I can write input from my >> index.html.erb form to that column. > > We call those 'tutorials'. Have you done any? If not, Google 'rails > tutorial'. In a very few hours you'll have learned how to do this and a > lot more.. > > Best regards, > Bill

Thanks for the reply, I have done the tutorials, but still am a bit confused.

Confused about what, specifically? Forms all default to using POST. If you have a form and it's not doing that, then we need to know: what does your code look like, what are you getting, what have you tried, etc.?

Thats why I was hoping for a quick example with the info I posted

You didn't post any information. You posted a request for a 'step by step on how to create a post action', which you can get very easily by simply scaffolding a 'dummy' model, which is what led me to suggest you try working thru a tutorial. Since you've already done that, try this:

ruby script/generate scaffold my_model test_param:string

Then look at the 'new' view and the 'create' method which the view 'feeds'.

Best regards. Bill

Hi Mark,

my controller looks like this:

What does the index method look like? That's the method that's feeding index.html.erb which appears to be the one that's causing you problems.

mystuff (controller)

def new     @post = Mystuff.new

      respond_to do |format|         format.html #new.html.erb         format.xml { render :xml => @post }       end     end

index.html.erb (view)

has this line in it:

<%= link_to 'New post', new_post_path %>

I get the following error:

undefined local variable or method `new_post_path' for #<ActionView::Base:0x384e54c>

So there's 'the beef.'

Extracted source (around line #79):

76: <img border="0" src="/images/botline.gif" width="41" height="12"></td> 77: </tr> 78: </table> 79: <%= link_to 'New post', new_post_path %> 80: 81: </body> 82:

For starters, run rake routes and let us know what it tells you. Either you have a route that understands 'new_post_path' or you don't. Probably not.

I appreciate you helping this newbie !!!!!

You're welcome. Sorry if I've seemed harsh.

Best regards, Bill

bill walton wrote:

Hi Mark,

my controller looks like this:

What does the index method look like? That's the method that's feeding index.html.erb which appears to be the one that's causing you problems.

undefined local variable or method `new_post_path' for #<ActionView::Base:0x384e54c>

So there's 'the beef.'

Extracted source (around line #79):

76: <img border="0" src="/images/botline.gif" width="41" height="12"></td> 77: </tr> 78: </table> 79: <%= link_to 'New post', new_post_path %> 80: 81: </body> 82:

For starters, run rake routes and let us know what it tells you. Either you have a route that understands 'new_post_path' or you don't. Probably not.

I appreciate you helping this newbie !!!!!

You're welcome. Sorry if I've seemed harsh.

Best regards, Bill

Bill,

This is the result of the rake routes:

mystuff_index GET /mystuff(.:format) {:controller=>"mystuff", :action=>"index"}               POST /mystuff(.:format) {:controller=>"mystuff", :action=>"create"}   new_mystuff GET /mystuff/new(.:format) {:controller=>"mystuff", :action=>"new"} edit_mystuff GET /mystuff/:id/edit(.:format) {:controller=>"mystuff", :action=>"edit"}       mystuff GET /mystuff/:id(.:format) {:controller=>"mystuff", :action=>"show"}               PUT /mystuff/:id(.:format) {:controller=>"mystuff", :action=>"update"}               DELETE /mystuff/:id(.:format) {:controller=>"mystuff", :action=>"destroy"}          root / {:controller=>"mystuff", :action=>"index"}                      /:controller/:action/:id                      /:controller/:action/:id(.:format)

Thanks again man!

Well on that line you are iterating though @posts but apparently you haven't set @posts to anything.

Fred

Hi Mark,

So I made the following changes:

<% form_for @post, :url => post_path %> <td><%=h post.m1 %></td> <%= link_to 'New post', new_mystuff_path %> <% end %>

and now get this error:

You have a nil object when you didn't expect it! You might have expected an instance of Array. The error occurred while evaluating nil.each

See Fred's response on this topic. Mine below addresses your original, I think.

by the way this is a cleaner view of the rake route:

OK. In the form above you're using two RESTful routes; post_path and new_mystuff_path. In your routes you have one of them.

  new_mystuff GET /mystuff/new(.:format) {:controller=>"mystuff", :action=>"new"}

The fact that you have no entries for post_path implies that you haven't added map.resources :post in routes.rb

The other question, and I apologize for not recognizing this yesterday, is 'what exactly are you trying to do?'

IIRC, your original question was about how to make a form POST data. But your questions have been about the link, not the form. Per the W3C standards, the default method for links is GET.

Are you trying to submit the form via a link? If so then you'll need to use link_to_remote and the :submit option.

If all you want to do is change the link's verb, then you need to add the :method option to the link_to.

HTH, Bill

Hi Mark,

Bill, thanks again for taking the time to respond.

You're welcome.

I have since been able to get the link_to from the index.html.erb to work and it sends the link to the new.html.erb page. Now though I get the following error:

NoMethodError in Mystuff#new

Showing app/views/mystuff/new.html.erb where line #3 raised:

undefined method `mystuffs_path' for #<ActionView::Base:0x368f5f8>

Extracted source (around line #3):

new.html.erb

1: <h1>Post Values to DB from stuff</h1> 2: 3: <% form_for(@post) do |f| %>

Looks like you've still got a problem with your routes. It's telling you it can't construct a mystuffs_path. But from line 3 above I'd expect it to be trying a post_path.

HTH, Bill

Hi Mark,

I guess I'm at a loss as to what to do, given the files I posted above and the routes.rb I thought I was doing it right.

"It's not what we don't know that hurts. It's what we know that ain't true." Will Rogers

I'd step back and revisit routes. This is a good start:

There are other good sources you can find via Google "rails routes"

Sorry I can't be more help at the moment.

Best regards, Bill