Controller/Actions not appearing on localhost - ruby-on-rails

I just created a new controller and a couple actions, but when I try to look at them on localhost I get the error: No route matches {:action=>"about", :controller=>"user"}
the controller looks like this:
class UserController < ApplicationController
def index
end
def register
#title = 'Register'
end
end
shouldn't I just be able to go http://localhost:3000/user/register and have something appear after creating a new controller?
What am I doing wrong here?
UPDATE - Added Routes.rb
Rails.application.routes.draw do
get 'user/index'
get 'user/register'
get 'site/index'
get 'site/about'
get 'site/help'
root 'site#index'
end

For the action to be accessible from the browser you will need to create routes to that endpoint. Here is a guide regarding routing

Related

Rails keeps displaying home page on root even when I delete the method in the Pages Controller

This is more of a question than a real issue, but I created a new rails project. I've changed the
Rails.application.routes.draw do
get 'pages/about'
get 'pages/contact'
devise_for :users
root to: 'pages#home'
end
and my Pages controller looks like this
class PagesController < ApplicationController
skip_before_action :authenticate_user!, only: [:home]
def home
end
def about
end
def contact
end
end
My homepage gets displayed even if I remove the home method.
I just wanted to know more about how this works.
I've looked around before posting and couldn't find an answer.
This is my first post here.
Thanks in advance
This is part of Rails 'convention over configuration' mantra. See Rendering by Default: Convention Over Configuration in Action
From the guide:
By default, controllers in Rails automatically render views with names that correspond to valid routes.
So, even if the home action is undefined, Rails will still render the home view because there is a valid route.

static pages in rails app missing template

I have to add a static page to my rails app.
Here's the logic I am following.
I made a directory called pages and put a file called signup.html.erb there.
The path in my another html file is pages/signup
In my routes.rb, I have get 'pages/signup'
I made a pages controller and there I have something like below
class PagesController < ApplicationController
def signup
end
end
I get below error.
Missing template pages/signup, application/signup with
What's wrong here?
just write in routs.rb
get '/pages/signup', to: 'pages#signup'
and in pages_controller.rb write
def signup
#page = Page.new
end
def create
# write create function
end
In my routes.rb, try
get 'pages/signup'
match "signup", :to => "pages#signup"
on rails 4
get 'pages/signup'
match "signup", :to => "pages#signup", via: :all
Because your controller isn't in the spree namespace change the full path of the html file from
app/views/spree/pages/signup.html.erb
to
app/views/pages/signup.html.erb

Index being redirected to Show View

When I go to /relationships/index it displays the show page even if it is not mentioned in the controller? Then when I try to just go to an index view without a show page created I get the following error: Unknown action: The action 'show' could not be found for relationships controller, even with no mention of it in the controller or a view file for the action.
routes.rb
Mymanual::Application.routes.draw do
resources :validation_rules
resources :validations
resources :product_types
resources :products
resources :connections
resources :relationships
root :to => 'products#index'
end
relationships_controller.rb
class RelationshipsController < ApplicationController
def index
end
def new
#relationship = Relationship.new
end
end
Then just HTML in a index.html.erb, show.html.erb and new.html.erb file.
It's because /relationship path is for relationships#index. If you go to /relationships/index, Rails router assumes you want to go to relationships#show path with params[:id] == 'index'.
Even though you want the index action, you don't enter index in the url. Otherwise, it thinks you're trying to find a Relationship with an id of index.
So just go to /relationships instead.
Go to a console and then run:
rake routes
It will give you all routes your app understand
In some place of this routes you will have something like:
relationships relationships#index /relationships
relationship relationships#show /relationships/:id
The issue that you have is that the app understand /relationships/index as relationships with id=index
The fix? use /relationships url

No route matches [GET] "/"

I'm new to rails so it may sound quite naive.I'm getting this error
No route matches [GET] "/"
Here is my routes.rb
MyApp::Application.routes.draw do
match 'welcome/contact' => 'welcome#index'
end
Here is my controller
class WelcomeController < ApplicationController
def index
redirect_to :action => :contact
end
def contact
end
end
And i have a contact.html.erb in my app/view/welcome/.What am i doing wrong?
I don't understand what you want to do. But I think you want your view Welcome/contact as your index page, if this is correct, you only have to change your routes.rb file like this:
root to: 'welcome#contact'
and you have to remove the index.html file from the public folder.
On the other hand, you can read more of rails routes here
You need to create a route for the actions other than CRUD actions in controller.This will solve the issue for all actions.
match ':controller(/:action)'
What you want to do is to RENDER the contact page, not to redirect to another controller and action.
Just put the code in the contact view into the app/views/welcome/index.html.erb file, and live happily.
You need to add a contact action to your WelcomeController
class WelcomeController < ApplicationController
def index
redirect_to :action => :contact
end
def contact
end
end

How to fix this up? " Unknown action The action 'index' could not be found for UsersController"

When I go to my localhost:3000/users page, I get:
Unknown action
The action 'index' could not be found for UsersController.
If you were following Hartl's tutorial,then accessing localhost:3000/users will cause this error. Try localhost:3000/signup instead.
You need not define the default actions (assuming appropriate http method), all you need to do is add the following to your config/routes.rb
resources :users
First you need to make sure that your controller actually has an index action, so
class UsersController < ApplicationController has to include the def index ... end in it.
Also, make sure your routes are set up correctly using
resources :users
and check it by typing
rake routes
in the terminal to check that the routes are right.
You might also want to check that the root is set up correctly in the config/routes.rb file
if you got the same problem with me (I cant access the localhost:3000/users but I can access my localhost:3000/signup), it might be works for u.
First, in your users_controller.rb (Controller for Users) , add
def index
end
Then , make a file "index/html/erb" in your app/views/users/index.html.erb
and put this code
<% controller.redirect_to "/signup" %>
You might have to rerun your server, and it works on my problem.
Remember, if you've included
get 'signup' => 'users#new
resources :users
…in your routes.rb file, then you need to use localhost:3000/signup instead. I believe if you removed get 'signup' => 'users#new and left only resources :users then using localhost:3000/users would take you to the new user signup form.
Remove the debugger line. Also, make sure you have exit the rails console.
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
debugger
end
def new
end
end

Resources