I am new to Ruby and Rails. This is my second application where I'm trying to implement login authentication.
Getting this error
No route matches "/user/process_login"
Here is my routes.rb
Myapp::Application.routes.draw do
get "user/login"
get "user/process_login"
end
On submit i am getting above error on login page. I think something has gone wrong in routes.rb or somewhere else.
looks like you're trying to post to that route,
try changing your routes file to this:
post "user/process_login"
You need to do this in your routes.rb
get "user/login" => "users#login"
get "user/process_login" => "users#process_login"
on a side note, I highly recommend that you use post instead of get to process the login.
Related
I've been away from Rails for a while but I have a site that is running on rails where I have created a page editor. I need some help with my routes.
I want to have something like this for my URL on the front end of the site:
//domain/welcome/pagename/page or //domain/welcome/page/pagename
Then within the welcome_controller I have this:
def page
# I provide the user with the a page
#mypage = Contents.find_by_name(params[:pagename])
end
Then within the page view itself I can render aspects of the #mypage record as I see fit.
However, I need some help with the routes to get this all working the way I intend.
Currently, I have this for my welcome route:
resources :welcome do
collection do
get :site
get :home
get :page
get :thankyou
post :newslettersignup
post :sendcontact
end
end
I know that I need more... Just now sure exactly what it should be.
Any help would greatly be appreciated!
You can use non-resourceful routing to do this easily:
get '/welcome/:pagename/site', to: 'welcome#site'
get '/welcome/:pagename/home', to: 'welcome#home'
get '/welcome/:pagename/page', to: 'welcome#page'
get '/welcome/:pagename/thankyou', to: 'welcome#thankyou'
post '/welcome/:pagename/newslettersignup', to: 'welcome#newslettersignup'
post '/welcome/:pagename/sendcontact', to: 'welcome#sendcontact'
The :pagename syntax allows the value of that segment of the URL to be included in params[:pagename]. The to: 'welcome#site' bit means, "Route this to the WelcomeController#site action."
Please see Rails Routing from the Outside In for a great reference when working with routes.
I am doing the very intro course in rails, and so far have completed the following
Made a new rails app: rails new shovell ("shovell" is the name of the app)
Installed all the gems and what not: bundle install
Generated a model rails generate model Story name link
Generated a controller rails generate controller Stories index
And now when I point to http://localhost:3000/stories, I get a error that says "Routing Error
No route matches [GET] "/stories" "
And the following is my routes.rb:
Shovell::Application.routes.draw do
get "stories/index"
# a bunch of comments
end
So I don't know what Im doing wrong, and why its not showing up the default welcome message, but rather giving me an error. Thanks for your help!
However, if you do:
http://localhost:3000/stories/index
you may get the page, although that's not the rails way.
First thing, read and understand the rails routing guide
Then in order to fix your code, on the routing you can write
Shovell::Aplication.routes.draw do
resources :stories
end
Or if you want custom routes instead of a rest resource
Shovel::Application.routes.draw do
match "stores", to: "my_controller#my_action"
end
And you can also name the custom route
Shovel::Application.routes.draw do
match "stores", to: "my_controller#my_action", as: :store_index
end
So with a name you can use the route name on your rails app
link_to("Store Index", store_index_path)
You've defined a route to /stories/index, but you've not defined one to /stories, which is why this is failing.
You should define this route like this:
get '/stories', :to => "stories#index"
For more information, see The Routing Guide
I have a users_controller. I've created a portfolios_controller which I would like to act similarly to the users_controller; so GETing /portfolios/10 would actually pull the user with an id of 10. I'm not quite sure how to set up my routes for this. Here's what I have right now in routes.rb:
get "portfolios/show"
resources :users
I got the following error when trying to access /portfolios/10: The action '10' could not be found for PortfoliosController.
Thoughts?
I would try a match statement with an :id parameter instead:
match "/portfolio/:id" => "portfolios#show"
Then you will have access to params[:id] in that action.
Total noob question.
I am building a simple photoblog in Rails which consists of Posts. Each post has its own id "/posts/1". I built Posts using rails scaffolding.
The problem is that I am unable to go to a url such as "/posts/index" or "/posts/anything" because it's trying to match anything after "/posts/" it to an id... So I get back an error like:
Couldn't find Post with ID=index
I'm sure this could be fixed with routes, but I'm not really sure how and I feel that there is some big-picture problem I am missing here.
You can clone my app from here: https://github.com/tbhockey/PhotoBlog
Thank you.
Running rake routes is usually pretty helpful. In this case, the route to your index should just be /posts (not /posts/index). If you run rake routes you'll probably see something like
posts GET /posts(.:format) {:action=>"index", :controller=>"posts"}
That's trying to tell you that the URL /posts will be directed to the index action of your posts_controller. Also, this may be referred to in your code as posts_path or posts_url.
Try to do this for your routes.rb :
scope :path => '/posts', :controller => :posts do
get 'show/(:id)' => :show, :as => 'show_post'
end
For a /posts/show/1, it will call the show action of the posts controller.
It's always a nice idea to learn about how routing works in Rails. It's very important and will help you understand many things about Rails. Especially looking at named routes is vital for good Rails programming.
I am working on a rails application and added a simple login system according to a book.
I created the controller admin:
rails generate controller admin login logout index
It added the following routes to routes.db
get "admin/login"
get "admin/logout"
get "admin/index"
I can got to http://localhost:3000/admin/login there is no problem at all.
But when I try to login I get: No route matches "/admin/login"!
Now, the first confusing part is that the "login" method of my AdminController is not executed at all.
The second confusing part is that this code works like a charm - redirects everything to /admin/login:
def authorize
unless User.find_by_id(session[:user_id])
flash[:notice] = "you need to login"
redirect_to :controller => 'admin', :action => 'login'
end
end
Sidenotes:
I restarted the server several times.
I tried a different browser - to be sure there is no caching problem.
Try
match "/admin/login" => "admin#login"
match "/admin/logout" => "admin#logout"
match "/admin/index" => "admin#index"
(notice the leading /)
As an aside, unless you're creating a login system to learn about Rails and/or authentication, you're probably better off using something like Devise.
Following on from David Sulc's answer:
You're defining the routes as get requests, meaning to go to them you must perform a GET /admin/login request which is basically what happens when you type the URL into your address bar or follow a link that uses it.
However when you try to use these URLs in a form, the form does a POST request and because you've defined all of these as get-only requests, Rails will not be able to find a compatible route.
I definitely agree with David that you should look at an alternative system such as Devise.