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,

On Wed, 2009-07-22 at 03:02 +0200, Mark P. wrote:

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

bill walton wrote:

Hi Mark,

On Wed, 2009-07-22 at 03:02 +0200, Mark P. wrote:

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. Thats why I was hoping for a quick example with the info I
posted

On Wed, 2009-07-22 at 03:56 +0200, Mark P. wrote:

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

bill walton wrote:

On Wed, 2009-07-22 at 03:56 +0200, Mark P. wrote:

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

my controller looks like this:

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

Extracted source (around line #79):

76:
77:
78:
79: <%= link_to ‘New post’, new_post_path %>
80:
81:
82:

I appreciate you helping this newbie !!!

Hi Mark,

On Thu, 2009-07-23 at 00:52 +0200, Mark P. wrote:

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:
77:
78:
79: <%= link_to ‘New post’, new_post_path %>
80:
81:
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,

On Thu, 2009-07-23 at 00:52 +0200, Mark P. wrote:

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:
77:
78:
79: <%= link_to ‘New post’, new_post_path %>
80:
81:
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!

Mark P. wrote:

bill walton wrote:

Hi Mark,

On Thu, 2009-07-23 at 00:52 +0200, Mark P. wrote:

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:
77:
78:
79: <%= link_to ‘New post’, new_post_path %>
80:
81:
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!

So I made the following changes:

<% form_for @post, :url => post_path %>

<%=h post.m1 %> <%= 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

Extracted source (around line #73):

70:
71: COPYRIGHT 2001 ALL RIGHTS RESERVED YOURDOMAIN.COM


72:
73: <% @posts.each do |post| %>
74:
75:

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

(in C:/rubyapps/stuff)

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)

On Jul 23, 5:11 am, Mark P. [email protected]
wrote:

You might have expected an instance of Array.
The error occurred while evaluating nil.each

Extracted source (around line #73):

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

Fred

Hi Mark,

On Thu, 2009-07-23 at 06:11 +0200, Mark P. wrote:

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

bill walton wrote:

Hi Mark,

On Thu, 2009-07-23 at 06:11 +0200, Mark P. wrote:

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

Bill, thanks again for taking the time to respond. 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:

Post Values to DB from stuff


2:
3: <% form_for(@post) do |f| %>
<%= f.error_messages %>

<%= f.label :M1 %>
<%= f.text_field :m1 %>

<%= f.submit 'Create' %>

<% end %>

<%= link_to ‘Back’, posts_path %>

Here is my mystuff controller:

require ‘Mystuff’

class MystuffController < ApplicationController

def index

  @my =  Mystuff.find(:all)
  @myposts = Mystuff.all

end


 def new
@post = Mystuff.new

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

 def create
@post = Mystuff.new(params[:myposts])

respond_to do |format|
  if @post.save
    flash[:notice] = 'Post was successfully created.'
    format.html { redirect_to(@post) }
    format.xml  { render :xml => @post, :status => :created, 

:location => @post }
else
format.html { render :action => “new” }
format.xml { render :xml => @post.errors, :status =>
:unprocessable_entity }
end
end
end

  def show
@post = Mystuff.find(params[:id])

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

end

  def edit
@post = Mystuff.find(params[:id])

end

I believe that once I get this fixed I’ll be done. Thanks again for
taking the time to help!

bill walton wrote:

Hi Mark,

On Thu, 2009-07-23 at 19:40 +0200, Mark P. wrote:

Bill, thanks again for taking the time to respond.

You’re welcome.

Extracted source (around line #3):

new.html.erb

1:

Post Values to DB from stuff


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

So my routes.rb looks like this:

routes.rb

ActionController::Routing::Routes.draw do |map|
map.resources :mystuff
map.root :controller => “mystuff”
map.connect ‘:controller/:action/:id’
map.connect ‘:controller/:action/:id.:format’
end

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.

Thanks

Hi Mark,

On Thu, 2009-07-23 at 19:40 +0200, Mark P. wrote:

Bill, thanks again for taking the time to respond.

You’re welcome.

Extracted source (around line #3):

new.html.erb

1:

Post Values to DB from stuff


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,

On Thu, 2009-07-23 at 20:31 +0200, Mark P. wrote:

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 R.

I’d step back and revisit routes. This is a good start:
http://guides.rubyonrails.org/routing.html

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