Undefined method?

I’m trying to learn rails as I go along, and having a bit of trouble.
There is an undefined method cropping that I don’t know why rails
thinks should be there.

Firstly, I’m using rails 3, ruby 1.9.2

I have a controller with an index action This part works fine, but i
am trying to add a comment form to the page that is rendered by that
index action.

Supposing my controller is named controller1,

I began this process first with:

rails g model comments name:string content:text
rake rb:migrate

Then I went on to define an action to create the comments:

within contoller1_controller.rb I added

def new
@comment = Comments.new

respond_to do |format|
  format.html
  format.xml { render :xml => @comment }
end

end

views/controller1/new.haml contains

= render ‘form’

and _form.html.erb contains

<%= form_for(@comment) do |format| %>

<%= format.label :name %>
<%= format.text_field :name %>

<%= format.label :content %>

<%= format.text_area :content %>


<%= format.submit %>

<% end %>

I then added into routes.db the following lines:
resource :comments
map.connect ‘/controller1/new’, :controller =>
‘controller1’, :action => ‘new’

Now, navigating to /controller1/new gives me this error:

undefined method `comments_index_path’

What am I missing?

If I change the form to “form_for(:Comments)”, then ‘/controller1/new’
renders, however, when I then add “@comments = Comments.all” into the
index definition, and:

  • @comments.each do |comment|
    • comment.content
      into the index.haml file, no comments are actually displayed. So I
      assume the form is not actually sending anything to the database

Any tips on what I’m doing wrong would be greatly appreciated,

Thanks

You were using a singular resource when you should have defined a plural
one. Also, you can specify the controller on your RESTful route instead
of having to create a separate map.connect or match:

resources :comments, :controller => ‘controller1’

I recommend that you use consistent naming with your controllers and
routes though.

HTH

On 10/14/10 5:53 PM, aperture science wrote:

=========
views/controller1/new.haml contains
<%= format.text_field :name %>

Any tips on what I’m doing wrong would be greatly appreciated,

Thanks


Erol F.

http://twitter.com/erolfornoles
http://ph.linkedin.com/in/erolfornoles

I’m still getting the “undefined method `comments_index_path’” error

The extracted source points at:
<%= form_for(@comment) do |format| %>

I cannot find where that method would be being called from
even…“comments_index_path” only appears in the development.log

Hmmm wait, I just noticed something. Could you rename your Comments
model to Comment (from plural to singular). Don’t forget to rename the
model file (comments.rb to comment.rb)

On 10/14/10 8:52 PM, aperture science wrote:

one. Also, you can specify the controller on your RESTful route instead

I have a controller with an index action This part works fine, but i
Then I went on to define an action to create the comments:
end

undefined method `comments_index_path’

Any tips on what I’m doing wrong would be greatly appreciated,

Thanks


Erol
Fornoleshttp://github.com/Erolhttp://twitter.com/erolfornoleshttp://ph.linkedin.com/in/erolfornoles


Erol F.

http://twitter.com/erolfornoles
http://ph.linkedin.com/in/erolfornoles

Thanks, that gets the comment form loading.

I added:

def create
@comment = Comment.new(params[:comments])

respond_to do |format|
  if @comment.save
    format.html { redirect_to('/', :notice => 'comment posted') }
    format.xml { render :xml => @comment, :status

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

to my controller1_controller.rb file, and now when I click the submit
button, I get the following error
“uninitialized constant CommentsController”

Despite the redirect_to(’/’, …) the page is redirecting to /comments

Can you post your route setup again?

On Fri, Oct 15, 2010 at 4:31 AM, aperture science
[email protected]wrote:

   format.xml { render :xml => @comment, :status

button, I get the following error

On 10/14/10 8:52 PM, aperture science wrote:

routes though.

end

<%= form_for(@comment) do |format| %>

- comment.content

.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


Erol M. Fornoles

http://twitter.com/erolfornoles
http://ph.linkedin.com/in/erolfornoles

Internet::Application.routes.draw do |map|
get “internets/index”
get “section4/index”
get “section3/index”
get “section2/index”
get “section1/index”
get “science/index”
resources :users
resources :comments
map.connect ‘/blog/new’, :controller => ‘section3’, :action => ‘new’
get ‘login(.:format)’ => ‘user_session#new’, :as => :login
post ‘login(.:format)’ => ‘user_session#create’, :as => :login
delete ‘logout(.:format)’ => ‘user_session#destroy’, :as => :logout
get “home/index”
root :to => “home#index”
map.logout ‘logout’, :controller => ‘user_session’, :action =>
‘destroy’
map.logout ‘logout’, :controller => ‘user_session’, :action =>
‘destroy’
map.edit ‘edit’, :controller => ‘users’, :action => ‘edit’
map.internets ‘internets’, :controller => ‘internets’, :action =>
‘index’
map.section4 ‘random’, :controller => ‘section4’, :action => ‘index’
map.section3 ‘blog’, :controller => ‘section3’, :action => ‘index’
map.section2 ‘artistry’, :controller => ‘section2’, :action =>
‘index’
map.section1 ‘home’, :controller => ‘section1’, :action => ‘index’
end

Thats the entirety of it. The area that is giving me trouble is in
the ‘section3’ controller

I can post comments from the console, and they do then show up in the
index.

and when I use “form_for :comments” the log shows the data being
posted, but there’s no INSERT INTO being executed

It seems like the form is not reading from the controller

thanks for the help

I got it to work by creating a comments_controller.rb file and using
the same action definitions as the section3_controller

I’m not sure why i had to make a new controller though.

But, thanks for the help