1

I'm trying to build a simple rails application however I get a routing error. Here is the controller:

class PostsController < ActionController::Base

  def index

    @var = "Rails is amazing"

  end



end

Here is the routing:

get "/posts", to: "posts#index"

And the routing error is as following:

uninitialized constant PostsController

The url im accessing is this one:

http://localhost:3000/posts#

I understand that controllers should be pluralised in both the routing and in the name of the file. I am sorry for such a novice question

1 Answer 1

1

I believe, you have posts_controller.rb file in the controllers folder.In the posts_controller.rb file add the following syntax

class PostsController < ApplicationController

end

In your routes file, try adding

resources :routes

In the terminal if you will type CONTROLLER=posts rake routes, you ll get the following output

Prefix Verb   URI Pattern               Controller#Action
    posts GET    /posts(.:format)          posts#index
          POST   /posts(.:format)          posts#create
 new_post GET    /posts/new(.:format)      posts#new
edit_post GET    /posts/:id/edit(.:format) posts#edit
     post GET    /posts/:id(.:format)      posts#show
          PATCH  /posts/:id(.:format)      posts#update
          PUT    /posts/:id(.:format)      posts#update
          DELETE /posts/:id(.:format)      posts#destroy
Sign up to request clarification or add additional context in comments.

3 Comments

Ah. So the file name must be posts_controller. I have given it the name of PostsController. That solved it. Thanks
one more quick question. It looks that when i do %= @var % The variable does not get printed into the view.
in your index.html.erb, you need to add <%= @var%>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.