Fun with Routing Errors

I’m at a complete loss. I can get to new and edit and when I submit the
new post or submit the edit, i’ll get:

Routing Error
No route matches “/knodes/new”

or

Routing Error
No route matches “/knodes/1/edit”

Here’s the controller:
class KnodesController < ApplicationController
before_filter :authenticate, :only => [:create, :destroy]
before_filter :authorized_user, :only => :destroy

GET /knodes

 # GET /knodes.xml

def index
@knodes = Knodes.all
end

GET /knodes/1

GET /knodes/1.xml

def show
@knodes = Knodes.find(params[:id])
end

GET /knodes/new

GET /knodes/new.xml

def new
@knodes = Knodes.new
end

GET /knodes/1/edit

def edit
@knodes = Knodes.find(params[:id])
end

POST /knodes

POST /posts.xml

def create
@knodes = current_user.knodes.build(params[:knodes])
if @knodes.save
redirect_to @user
else
render ‘edit’
end
end

 # PUT /knodes/1

PUT /knodes/1.xml

def update
@knodes = Knodes.find(params[:id])

 if @knodes.update_attributes(params[:knodes])
   redirect_to @knodes
 else
   render 'edit'
 end

end

DELETE /knodes/1

DELETE /knodes1.xml

def destroy
@knodes = Knodes.find(params[:id])
@knodes.destroy

 respond_to do |format|
   format.html { redirect_to(knodes_url) }
   format.xml  { head :ok }
 end

end

private

def authorized_user
@knodes = Knodes.find(params[:id])
redirect_to root_path unless current_user?(@knodes.user)
end
end

And my routes

sessions_new GET /sessions/new(.:format)
{:controller=>“sessions”, :action=>“new”}
knodes_new GET /knodes/new(.:format)
{:controller=>“knodes”, :action=>“new”}
users_new GET /users/new(.:format) {:controller=>“users”,
:action=>“new”}
pages_home GET /pages/home(.:format) {:controller=>“pages”,
:action=>“home”}
pages_about GET /pages/about(.:format) {:controller=>“pages”,
:action=>“about”}
pages_contact GET /pages/contact(.:format) {:controller=>“pages”,
:action=>“contact”}
pages_changelog GET /pages/changelog(.:format) {:controller=>“pages”,
:action=>“changelog”}
contact /contact(.:format) {:controller=>“pages”,
:action=>“contact”}
about /about(.:format) {:controller=>“pages”,
:action=>“about”}
signup /signup(.:format) {:controller=>“users”,
:action=>“new”}
signin /signin(.:format)
{:controller=>“sessions”, :action=>“new”}
signout /signout(.:format)
{:controller=>“sessions”, :action=>“destroy”}
changelog /changelog(.:format) {:controller=>“pages”,
:action=>“changelog”}
knodes GET /knodes(.:format)
{:controller=>“knodes”, :action=>“index”}
knodes POST /knodes(.:format)
{:controller=>“knodes”, :action=>“create”}
new_knode GET /knodes/new(.:format)
{:controller=>“knodes”, :action=>“new”}
edit_knode GET /knodes/:id/edit(.:format)
{:controller=>“knodes”, :action=>“edit”}
knode GET /knodes/:id(.:format)
{:controller=>“knodes”, :action=>“show”}
knode PUT /knodes/:id(.:format)
{:controller=>“knodes”, :action=>“update”}
knode DELETE /knodes/:id(.:format)
{:controller=>“knodes”, :action=>“destroy”}
users GET /users(.:format) {:controller=>“users”,
:action=>“index”}
users POST /users(.:format) {:controller=>“users”,
:action=>“create”}
new_user GET /users/new(.:format) {:controller=>“users”,
:action=>“new”}
edit_user GET /users/:id/edit(.:format) {:controller=>“users”,
:action=>“edit”}
user GET /users/:id(.:format) {:controller=>“users”,
:action=>“show”}
user PUT /users/:id(.:format) {:controller=>“users”,
:action=>“update”}
user DELETE /users/:id(.:format) {:controller=>“users”,
:action=>“destroy”}
sessions GET /sessions(.:format)
{:controller=>“sessions”, :action=>“index”}
sessions POST /sessions(.:format)
{:controller=>“sessions”, :action=>“create”}
new_session GET /sessions/new(.:format)
{:controller=>“sessions”, :action=>“new”}
session DELETE /sessions/:id(.:format)
{:controller=>“sessions”, :action=>“destroy”}
root /(.:format) {:controller=>“pages”,
:action=>“home”}

Also this is my first post on the ruby-forum! I hate that it’s for a
problem i’m having. I’ve been looking for a good ruby/rails forum for a
while.

HOWDY!

On 25 November 2010 22:06, Ben W. [email protected] wrote:

I’m at a complete loss. I can get to new and edit and when I submit the
new post or submit the edit, i’ll get:

Routing Error
No route matches “/knodes/new”

Are you saying that you can get to new ok, so you are in the view
new.html.erb, then when you click the save button you get the routing
error above? that seems a bit odd as it should be going to create not
new. Have you checked in log/development.log to see if there is any
more info? It may be that it is going to the create and then failing
at the next stage. Does it create the new record? If you are sure it
is not going to create then post the view code please.

or

Routing Error
No route matches “/knodes/1/edit”

Similarly this should be going to update not edit.

Colin

Thanks for the response! To clarify:

I can actually get to new.html.erb and :id/edit.html.erb. the problem
is on submit i’ll get those errors. it does not create or edit the post
when i submit either. I have manually made 2 posts in mysql for testing
purposes.

Here is the newest lines in my log/dev:

Started POST “/knodes/2/edit” for 72.1.183.251 at Thu Nov 25 13:35:41
-0800 2010

ActionController::RoutingError (No route matches “/knodes/2/edit”):

Rendered
/home/bwoodall/.gem/ruby/1.8/gems/actionpack-3.0.1/lib/action_dispatch/middleware/templates/rescues/routing_error.erb
within rescues/layout (1.0ms)

Started GET “/knodes” for 72.1.183.251 at Thu Nov 25 13:38:35 -0800 2010
Processing by KnodesController#index as HTML
Knodes Load (0.2ms) SELECT knodes.* FROM knodes ORDER BY
knodes.created_at DESC
Rendered layouts/_stylesheets.html.erb (2.4ms)
User Load (0.2ms) SELECT users.* FROM users WHERE (users.id =

  1. LIMIT 1
    Rendered layouts/_header.html.erb (41.5ms)
    Rendered layouts/_footer.html.erb (1.9ms)
    Rendered knodes/index.html.erb within layouts/application (73.0ms)
    Completed 200 OK in 98ms (Views: 78.5ms | ActiveRecord: 0.4ms)

Started GET “/knodes/new” for 72.1.183.251 at Thu Nov 25 13:38:37 -0800
2010
Processing by KnodesController#new as HTML
User Load (0.1ms) SELECT users.* FROM users WHERE (users.id =

  1. LIMIT 1
    DEPRECATION WARNING: <% %> style block helpers are deprecated. Please
    use <%= %>. (called from
    _app_views_knodes__new_form_html_erb__707979935__617958448_1646016 at
    /home/bwoodall/rails/knode/app/views/knodes/_new_form.html.erb:1)
    Rendered knodes/_new_form.html.erb (12.7ms)
    Rendered layouts/_stylesheets.html.erb (1.9ms)
    Rendered layouts/_header.html.erb (4.8ms)
    Rendered layouts/_footer.html.erb (1.8ms)
    Rendered knodes/new.html.erb within layouts/application (29.6ms)
    Completed 200 OK in 77ms (Views: 33.9ms | ActiveRecord: 0.1ms)

Started POST “/knodes/new” for 72.1.183.251 at Thu Nov 25 13:38:46 -0800
2010

ActionController::RoutingError (No route matches “/knodes/new”):

Rendered
/home/bwoodall/.gem/ruby/1.8/gems/actionpack-3.0.1/lib/action_dispatch/middleware/templates/rescues/routing_error.erb
within rescues/layout (1.2ms)

Started POST “/knodes/new” for 72.1.183.251 at Thu Nov 25 13:43:03 -0800
2010

ActionController::RoutingError (No route matches “/knodes/new”):

Rendered
/home/bwoodall/.gem/ruby/1.8/gems/actionpack-3.0.1/lib/action_dispatch/middleware/templates/rescues/routing_error.erb
within rescues/layout (1.3ms)

knodes/_form.html.erb

<% form_for @knodes, :url => { :action => :edit } do |f| %>
<% if @knodes.errors.any? %>


<%= pluralize(@knodes.errors.count, “error”) %> prohibited
this knode from being saved:

  <ul>
  <% @knodes.errors.full_messages.each do |msg| %>
    <li><%= msg %></li>
  <% end %>
  </ul>
</div>

<% end %>

<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :info %>
<%= h f.text_area :info, :class => "mceEditor" %>
<%= f.label :media %>
<%= h f.text_area :media, :class => "mceEditor" %>
<%= f.label :links %>
<%= h f.text_area :links, :class => "mceEditor" %>
<%= f.submit %>
<% end %> =========================== edit.html.erb ===========================

Editing Knode

<%= render ‘form’ %>

<%= link_to ‘Show’, @knodes %> |
<%= link_to ‘Back’, knodes_path %>

It’s because of this line in the _form.html.erb…isn’t it…

<% form_for @knodes, :url => { :action => :edit } do |f| %>

the { :action => :edit } is keeping it on edit and not pushing it to
update

i put that in there for edit and new because without it for
new.html.erb, i keep getting:

===============================
NoMethodError in Knodes#new

Showing …/app/views/knodes/_new_form.html.erb where line #1 raised:

undefined method `knodes_index_path’ for
#<#Class:0xb65df780:0xb65d71d4>

Extracted source (around line #1):

1: <% form_for @knodes do |f| %>
2: <% if @knodes.errors.any? %>
3:

4:

<%= pluralize(@knodes.errors.count, “error”) %> prohibited
this knode from being saved:

So I guess this is a new problem all together. Taking the line:

{ :action => :edit }

out of the _form.html.erb for edit makes it work btw. I guess my
problem is with the NoMethodError in Knodes#new

On 26 November 2010 14:22, Ben W. [email protected] wrote:

out of the _form.html.erb for edit makes it work btw. I guess my
problem is with the NoMethodError in Knodes#new

I am a bit confused about you naming for Knodes. In an earlier post you
have
def new
@knodes = Knodes.new
end
which suggests that your model is class Knodes, and you seem to be
using knodes for both the singular and plural. Rails can get confused
if you do not follow the conventions, though it is possible to work
around this. I would have expected, though, to see model Knode in
model/knode.rb, and controller KnodesController in
controllers/knodes_controller.rb. Whether this is part of your
problem I do not know.

Colin

On Nov 26, 1:53pm, Ben W. [email protected] wrote:

=====================
<% form_for @knodes, :url => { :action => :edit } do |f| %>

This is incorrect - your form should be submitting to the update or
create action depending on whether the object is saved or not.
If you just do <% form_for @knodes do |f| %> rails will handle this
for you

Fred

Frederick C. wrote in post #964102:

On Nov 26, 1:53pm, Ben W. [email protected] wrote:

=====================
<% form_for @knodes, :url => { :action => :edit } do |f| %>

This is incorrect - your form should be submitting to the update or
create action depending on whether the object is saved or not.
If you just do <% form_for @knodes do |f| %> rails will handle this
for you

Fred

Yeah, that’s the woeful conclusion I came to. As posted right before
you did, without that line in _new_form.html.erb for a new post, I keep
getting:

===============================
NoMethodError in Knodes#new

Showing …/app/views/knodes/_new_form.html.erb where line #1 raised:

undefined method `knodes_index_path’ for
#<#Class:0xb65df780:0xb65d71d4>

Extracted source (around line #1):

1: <% form_for @knodes do |f| %>
2: <% if @knodes.errors.any? %>
3:

4:

<%= pluralize(@knodes.errors.count, “error”) %> prohibited
this knode from being saved:

So this is a new problem than the one I first submitted. Board
etiquette: should I keep this post going for this new problem, or submit
a new one?

I really appreciate the help i’ve gotten so far btw.

On 26 November 2010 14:42, Ben W. [email protected] wrote:

@knodes = Knodes.new

Yeah…this is only my second rails project and I thinking that this is
my problem. I screwed up on the naming conventions. Honestly, if you
were to just tell me “this is your problem, fix it cuz there’s nothing
else we can help you with” i’d pretty much understand. I think i’m
going to have to start over on this and change the names around

I not going to guarantee this is the problem (in fact I suspect it may
not be), but if it isn’t you will probably run into some problem with
the names at some point if you don’t fix it now. The less code there
is the easier to change. In addition to the model name I would
suggest also making the variable names consistent. So for example it
would be better if it were
@knode = Knode.new
and
@knodes = Knode.all
Apart from anything else, when you are writing code you will always
know that @knode is a single item and @knodes is a set.

Colin

Colin L. wrote in post #964106:

On 26 November 2010 14:22, Ben W. [email protected] wrote:

out of the _form.html.erb for edit makes it work btw. I guess my
problem is with the NoMethodError in Knodes#new

I am a bit confused about you naming for Knodes. In an earlier post you
have
def new
@knodes = Knodes.new
end
which suggests that your model is class Knodes, and you seem to be
using knodes for both the singular and plural. Rails can get confused
if you do not follow the conventions, though it is possible to work
around this. I would have expected, though, to see model Knode in
model/knode.rb, and controller KnodesController in
controllers/knodes_controller.rb. Whether this is part of your
problem I do not know.

Colin

Yeah…this is only my second rails project and I thinking that this is
my problem. I screwed up on the naming conventions. Honestly, if you
were to just tell me “this is your problem, fix it cuz there’s nothing
else we can help you with” i’d pretty much understand. I think i’m
going to have to start over on this and change the names around