droptheplot
droptheplot

Reputation: 628

RoR controllers return 404 error

Hello and here is my problem.

All controllers return always 404 error in browser, but in logs:

Processing PostController#index (for myip at 2013-02-01 13:33:02) [GET]
Rendering post/index
Completed in 2ms (View: 1, DB: 0) | 200 OK [http://site.com/]

And files in /public load fine. My routes.rb:

map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'

Hope for your help.

Upvotes: 0

Views: 358

Answers (2)

Zedrian
Zedrian

Reputation: 909

A simple way to have your controllers work is to add resources in your routes. This will map all your controller methods.

# app/controllers/controllernames_controller.rb
class ControllernamesController < ApplicationController
  def index
  end

  # and other methods you want...
end

# config/routes.rb
MyApp::Application.routes.draw do
   resources :controllername
end

Your links would then become http://localhost/controllernames/methodname/id

Then in your view files you can add links the following way:

<%= link_to "whatever_your_like_to_name", controllernames_path %>

If you use the RESTful scaffolding that Rails provides you can then do:

<%= link_to "whatever_your_like_to_name", new_controllername_path %>
# or
<%= link_to "whatever_your_like_to_name", edit_controllername_path(controllername) %>
# or 
etc...

another way to get a single method from a controller you can do the following your routes:

# config/routes.rb
MyApp::Application.routes.draw do
   get 'controllernames/methodname', to: 'controllernames#methodname', as: "whatever_you_want"
end

In this case your link in the view file would be

<%= link_to "whatever_you_like_to_name", whatever_you_want_path %>

Upvotes: 0

jvnill
jvnill

Reputation: 29599

your routes mean that you need to always match the following /controller/action/id or /controller/action/id.format. You should use parenthesis like the one generated for a new rails project. take note of the comment on why you should not do this

# This is a legacy wild controller route that's not recommended for RESTful  applications.
# Note: This route will make all actions in every controller accessible via GET requests.
# match ':controller(/:action(/:id))(.:format)'

Upvotes: 1

Related Questions