Showing username in url without controller name - ruby-on-rails

Hope everyone's good this morning. Been learning rails lately, and been working on a little "social" app for practice. I've looked around for a good answer for this, but couldn't find the right thing for me. Here's what I'm trying to figure out now:
By default, the url is hi.com/user/12, and so far I've changed that to /user/name. Everything works fine there. But of course, I want to take it further and do hi.com/name (mostly because I want to learn how it's done properly).
Here's my (abbreviated) routes:
resources :users do
member do
get :following, :followers
end
end
root to: 'pages#home'
match '/help', to: 'pages#help'
match '/about', to: 'pages#about'
match '/contact', to: 'pages#contact'
match '/signup', to: 'users#new'
match '/signin', to: 'sessions#new'
match '/signout', to: 'sessions#destroy', via: :delete
Here's the show view from the controller:
def show
#user = User.find_by_username(params[:id])
#tweets = #user.tweets.paginate(page: params[:page])
end
Now what I've tried is to stick match ':username', to: 'users#show' in my routes, and then change find_by_username(params[:id]) in the controller to find_by_username(params[:username]). This actually works really well, and shows all proper user info on hi.com/name. The following and followers are still (as expected) on user/name/following (and they work fine). However, all static pages break, including /signout (kinda important :p), and while /users still shows an index of all users, users/name throws an error (if /name works, then I wouldn't need /users/name anyway). The error being undefined method `tweets' for nil:NilClass in the users controller.
Again what I'm trying to accomplish is hi.com/username and hi.com/username/following, etc. Also need my static pages to still work. I have everything working with /users/name, but I want to learn how to make it work this way too.
Thanks everyone, I appreciate it!

Basically, it's easy but it has side effects.
Just add at the end of your routes.rb:
get '/:id', to: 'user#show'
But, a user named about, or users or pages won't be able to see their profile, because the path is used earlier in your routes.rb.

Related

My routes file doesn't match expected result

Thanks in advance for any help.
I'm working my way through Michael Hartl's "learn enough" RoR lessons and am at the point where we're working with authenticating users via sessions.
Per the lesson, I've just run rails generate controller Sessions new and nothing blew chunks:
$ rails generate controller Sessions new
Running via Spring preloader in process 4407
create app/controllers/sessions_controller.rb
route get 'sessions/new'
invoke erb
create app/views/sessions
create app/views/sessions/new.html.erb
invoke test_unit
create test/controllers/sessions_controller_test.rb
invoke helper
create app/helpers/sessions_helper.rb
invoke test_unit
invoke assets
invoke coffee
create app/assets/javascripts/sessions.coffee
invoke scss
create app/assets/stylesheets/sessions.scss
However, there's a discrepancy between what the lesson says routes.rb should look like and what it actually does look like.
Expected result in routes.rb, as implied by the lesson:
Rails.application.routes.draw do
root 'static_pages#home'
get '/help', to: 'static_pages#help'
get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact'
get '/signup', to: 'users#new'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
resources :users
end
Actual result in routes.rb:
Rails.application.routes.draw do
get 'sessions/new'
root 'static_pages#home'
get '/help', to: 'static_pages#help'
get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact'
get '/signup', to: 'users#new'
post '/signup', to: 'users#create'
resources :users
end
Am I missing something obvious? Why the difference?
I feel like there's a missing chapter between here and the last one, which only explains the really high level difference between browser-close-ending sessions and cookie-expired sessions.
You ran rails generate controller Sessions new, which means you want to generate a controller with the new action only. By default, new generates the view route (GET) and the actual resource creation route (POST).
To create the controller and routes following the CRUD pattern, you should run rails generate controller Sessions.
Solved.
The reason for the discrepancy is that rails generate controller Sessions new only generates the GET route #vnbrs mentioned.
The lesson calls for 3 routes that need to be supported: a GET request to the login form, a POST request to create the session, and a DELETE request to destroy the session.
Those need to be added to routes.rb manually:
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
Beginner's disorientation, I couldn't see the forest for the trees and broke my brain. (P.S. I don't recommend trying to tackle this tutorial discontinuously over a series of months...your brain does NOT pick up where it left off.)

routing error for root and get pages

I am developing a rails app and This is my first time working with ruby or any server-side programming language. I connected to localhost:3000 and everything went well. I went to change the routing for my home page as well as create some new ones but I'm having an issue of gettign them to preview and I keep Getting error messages. Here is the snippet from my routes.rb file
My routes.rb
Rails.application.routes.draw do
root 'pages#home'
get 'about' => 'pages#about'
post 'forum' => 'pages#forum'
get 'contact-us' => 'pages#contact-us'
end
I'm still relatively new To programming as well as stack overflow so I can't embed images yet So it will only have a link. Any help would be appreciated. Thank you!
1 - Try to avoid fat arrow
2- Controller name should not contain -(dash) instead use _(Underscore) example(contact-us should be contact_us)
Rails.application.routes.draw do
root 'pages#home'
get '/about', to: 'pages#about', as: :about
post '/forum', to: 'pages#forum', as: :forum
get '/contact_us', to: 'pages#contact_us', as: :contact_us
end
3- After changing this run rake routes at your console in your project directory , you will get list of routes
4- you can now use routes helper of prefix_path example (root_path, about_path or forum_path, contact_path)

Michael Hartl ch9 : how to sign up

I’ve completed Michael Hartl ch 9 and have no error in testing.
But I could not sign up...
It returns error as below even so previously I be able to sign up..login works fine.
How to fix it and be able to sign up?
Should I merge sign up branch? But doing that I could loose all changes.. Could something be done about sign up form this more advance branch?
Error:
No route matches [POST] "/signup"
routes.rb:
Rails.application.routes.draw do
root 'static_pages#home'
get '/help', to: 'static_pages#help'
get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact'
get '/signup', to: 'users#new'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
resources :users
end
thanks.
Thanks everybody for answers.
It works now.
One more question related.
When users sign up - they could directly participate in website. It is no good since they need to get confirmation by email and only that they could participate.
How it could be implemented?
I think it is in the solutions Michael Hartl and I even implemented it for Rails 4, but could not remember what need to change..
What files need to be changed to allow users participate in website only after getting confirmation by email.
thanks.
The error is very clear and crisp, you don't have a route for users#create, you need to implement that action and it should work.
Go through this section in the book..
No route matches [POST] "/signup"
You should have post '/signup', to: 'users#create' in your routes.
You need to add
post '/signup', to: 'users#create'
And write create method in
class UsersController < ApplicationController
def create
# Create User with signup parameterss
end
end

Matching and Routes in Rails

I generated a controller and changed the routes but opening the links yields errors on my local server.
Generating controller and routes
rails generate controller StaticPages home about team contact
Change routes.rb
MyApp::Application.routes.draw do
root to: 'static_pages#home'
match '/about', to: 'static_pages#about'
match '/team', to: 'static_pages#team'
match '/contact', to: 'static_pages#contact'
end
The root path work but none of the 'about, 'team', or 'contact' links work. This is the error I get:
"You should not use the match method in your router without specifying an HTTP method. If you want to expose your action to both GET and POST, add via: [:get, :post] option. If you want to expose your action to GET, use get in the router: Instead of: match "controller#action" Do: get "controller#action""
Why can't I use 'match'?
match method has been deprecated.
Use get for GET and post for POST.
get '/about', to: 'static_pages#about'
You can use match, you've gotta add a via: option:
match '/about', to: 'static_pages#about', via: :get
match '/team', to: 'static_pages#team', via: :get
match '/contact', to: 'static_pages#contact', via: :get
You can also pass other HTTP verbs to via: if you need to, like via: [:get, :post]
Source: Rails Routing Guide
First, you must specify the HTTP method by adding via: :get at the end of match 'st' => 'controller#action
And it's better to use get '/home', to: 'static_pages#home'
But, there is a problem, that your code doesn't follow RESTful, that only support 7 actions: index, new, edit, create, update, show and destroy.
These are 2 solutions:
SOL 1: Put them in different controller (homes, abouts..) and all of these controllers have action index.
SOL 2: If it's too much work, we can match them to show action. We use static_pages controller, and each page (home, about) will be a item.
The routes will look likes
/static_pages/home
/static_pages/about
I know it isn't good because of the prefix static_pages.
We can easily get rid of this by adding a custom routes at the end of routes file:
get '/:id', to: 'static_pages#show'
That's it. And if you think it's too much work (I think so too), check out this gem High Voltage. Have fun.

Error following Michael Hartl's tutorial:

I've been following Michael Hartl's rails tutorial but without testing (bad practice I know - I'm completely new to web programming having only dabbled in HTML and CSS before). I've reached the last chapter but I'm having problems with the user signup form. It's rendering properly in the browser but on submit I get the message
No route matches "/users/new"
Everything seems to be as it should be in the routes.rb file, and by entering users/new directly into the browser I can navigate to the correct page (the signup form) - but can't create new users.
There doesn't seem to be anything missed out from Michael Hartl's code
I've checked out the users controller as I figured it must be something to do with the 'new' or 'create' actions. It might also have to do with the number of "swap" files that seem to be being created every time I edit a file with Vim. I'm completely ignorant about what this means, but perhaps it's screwing things up. I've left these intact in my github push so you can see my incompetence.
Thanks for any help you can give me!
Here's the routes.rb file (everything else is on github at https://github.com/jnwb2/the_app):
TheApp::Application.routes.draw do
resources :users do
member do
get :following, :followers
end
end
resources :sessions, :only => [:new, :create, :destroy]
resources :microposts, :only => [:create, :destroy]
resources :relationships, :only => [:create, :destroy]
match '/contact', :to => 'pages#contact'
match '/about', :to => 'pages#about'
match '/help', :to => 'pages#help'
match '/signup', :to => 'users#new'
match '/signin', :to => 'sessions#new'
match '/signout', :to => 'sessions#destroy'
root :to => 'pages#home'
end
Found the answer. For one, i think you should stop following this tutorial. It seems to have a lot of bad practices going on. Try going to guides.rubyonrails.org instead. To fix your problem however, change line #8 in the users controller like so:
def new
#title = "Sign up"
#user = User.new
end
Sorry for all my extra comments from before, just ended up cloning your source to find the issue.

Resources