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)
Related
I have done it before but am having trouble adding a new page and a new path to my rails server.
Pretty much, I want to add a new page and then link to that page in a drop down menu... but I am having trouble getting the changes to take effect and for the new path/route to show up when I do "rails routes".
I have done it before for an "offerings" page at pages#offerings but can't seem to figure out how to repeat the same process
I started off going to the pages controller and adding a "def public_speaking" and "end":
Pages Controller
# GET request for / which is our home page
def home
#basic_plan = Plan.find(1)
#pro_plan = Plan.find(2)
end
def about
end
def offerings
end
def public_speaking
end
end
Routes.rb
Then in Routes.rb I tried using the same process (Adding get 'public_speaking', to : 'pages#public_speaking')
root to: "pages#home"
devise_for :users, controllers: { registrations: 'users/registrations' }
resources :users do
resource :profile
end
get 'about', to: 'pages#about'
resources :contacts, only: [:create]
get 'contact-us', to: 'contacts#new', as: 'new_contact'
get 'offerings', to: 'pages#offerings'
get 'public_speaking', 'pages#public_speaking'
end
View file
I also created a file "public_speaking.html.erb" in the views folder with the same name.
What am I doing wrong/missing to create this new path? Is there some command to execute this linkage or something?
I expected there to be a new route created (since it worked for "offerings"), however it has not worked and I'm not sure why. I will be repeating this process for 5-6 pages, so I want to be sure I can do it right
i see in your routes, it seems your code is not correct.
you should change:
from get 'public_speaking', 'pages#public_speaking'
to get 'public_speaking', to: 'pages#public_speaking'
Khan Pham has given a correct answer. Seems you are messing with link.
Accordingly to Ruby on rails guide the proper route would be:
get 'public_speaking', to: 'pages#public_speaking'
where to: expects controller#action format.
And then you can check your routes by executing command rake routes and if your part presents there you can use it in your views like:
link_to('Public Speaking', public_speaking_path)
you can read more about url here. Good luck!
using
Rails 5.1.4 &
ruby 2.4.1
Wondering why Rails would not give a destroy_visitor_path for DELETE request. Any info would be greatly appreciated. Thank you!
routes.rb
Rails.application.routes.draw do
root :to => 'contents#index'
resources :contents
resources :visitors
end
Routes:
visitors GET /visitors(.:format) visitors#index
POST /visitors(.:format) visitors#create
new_visitor GET /visitors/new(.:format) visitors#new
edit_visitor GET /visitors/:id/edit(.:format) visitors#edit
visitor GET /visitors/:id(.:format) visitors#show
PATCH /visitors/:id(.:format) visitors#update
PUT /visitors/:id(.:format) visitors#update
DELETE /visitors/:id(.:format) visitors#destroy
Same reason why there's no update_visitor_path — it's a singular resource that simply responds to a different HTTP verb.
In your view, you'd link_to "Delete", visitor_path, method: :delete
I can't comment on the philosophy behind the design decision, or any of the arguments about whatever spec it's designed to because that's for people who are smarter than I am.
More info on the Rails routing guide: http://guides.rubyonrails.org/routing.html
There are many incidents to delete
It's let you set your own routes:
For example you want to logout after delete (usually in sessions)
get '/logout' => 'sessions#destroy', :as => 'logout'
I am getting the following error only on occasion when I attempt to load up my Rails app in localhost.
Invalid route name, already in use: 'root' You may have defined two routes with the same name using the :as option, or you may be overriding a route already defined by a resource with the same naming. For the latter, you can restrict the routes created with resources as explained here: http://guides.rubyonrails.org/routing.html#restricting-the-routes-created
For some reason, it only happens every now and again, and generally goes away after I refresh the page once. The file it is calling into question has this as the code (line causing the error indicated):
Rails.application.routes.draw do
get 'welcome/index'
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
Rails.application.routes do
resources :articles
root 'welcome#index' #-->This is the line that causes the error
end
resources :articles do
resources :comments
end
end
I am really new to Rails, and I'm not sure what is causing the problem or if would even be a problem if I were to actually host this beginner project on the web. Also, if it helps, I am running Ruby 2.2.2 and Rails 4.2.1.
Thank you in advance for the help!
You have (Rails.application.routes) nested inside (Rails.application.routes.draw). Run rake routes and you will see that you have resources for articles twice. Furthermore, you have root 'welcome#index' nested inside and that is why you are getting the error. Your routes should look like this
Rails.application.routes.draw do
get 'welcome/index' => 'welcome#index'
root 'welcome#index'
resources :articles do
resources :comments
end
end
Note that the route of your application meaning(/) and (/welcome/index) both point to the welcome controller index action. You don't really need get 'welcome/index' => 'welcome#index' and you can delete that line and use root when ever you need the index action from the welcome controller.
Try using the "Full" syntax.
root :to=> "some#action"
You have two routes pointing to the same page index, get rid of your get route
get 'welcome/index'
Also your root route is nested that should be moved outside since the root route is the root of your entire app and not a specific resource
I have a website with a controller named "Posts".
I would like to redirect "example.com/posts" to "example.com" as they display the same information.
I know this is done in the routes.rb file but after a few hours of searching I don't think I'll figure it out. Any help is appreciated, thanks!
I am using Rails 4.0 on Ruby 2.0
You can read all about redirection in routes, here: http://guides.rubyonrails.org/routing.html#redirection
get '/posts', to: redirect('/')
...
root to: 'posts#index'
In your routes.rb file make sure the root :to is at the top of your code. Then you would need to assign a redirect for the GET request you want to always sent there:
root :to => 'posts#index'
get '/posts', to: redirect('/')
I'm new to ruby on rails, and I'm having trouble getting up to speed. I've set up devise for user authentication, and everything seemed to be going well. Until I tried to get routes set up
Basically, I've deleted my /public/index.html, and I've set a new default route. Here's my /config/routes.rb
MyApp::Application.routes.draw do
authenticated :user do
root :to => 'home#index'
end
root :to => "home#index"
devise_for :users
end
Okay, so that seems simple enough. I generated a home controller/view. My index.html.erb is just the default. My controller is also blank.
When I run rake routes, I can see the route to the root.
root / home#index
root / home#index
So this seems like a go! But...It's not. I get an error when I try and open my index
No route matches [GET] "/"
I don't feel like the details of my server should be significant, but it's nginx. I can post my server configuration if anyone thinks it's important.
Blargh. I'm an idiot. The server configuration I though wouldn't matter was the problem. I had edited my nginx settings, but I left the default in Unicorn. I never noticed before now, since the /public/index.html in both the unnecessary app and my app were the same. Thanks for all the help though.
You should be add resources :homes
MyApp::Application.routes.draw do
resources :homes
root :to => "home#index"
devise_for :users
end