I am working on a Ruby on Rails project. In the project I generated a controller with a name controller1 which automatically gives me my view files (show, index, and others). This also gives me my url (http://www,website.com/controller1/).
But now I want to change the URL path to another one (http://www.website.com/controls). Is there a way I can do this without having to write route command for each file in the controller1 view folder.
Oh! And I will appreciate options better than the "Moved Permanently" redirect route command code.
Thanks in advance.
Add this to your routes.rb
resource :controls, controller: 'controller1' # add this line
resource :controller1 # possibly this is in your routes.rb already
Reference: Specifying a Controller to Use
Related
I have a messy rails 3 application that's come from a different developer and I need to refactor it.
What I want to do is move the contents of "app" into a subfolder called "classic".
app/classic
And then have all URL's with a classic prefix such as
localhost:3000/classic/wills/new
Route to controllers inside of the "app/classic" folder.
And then every regular url that does not contain the classic prefix - route in the standard way to app/
Is it possible to do this? The only thing I've discovered so far is that I can add a scope inside of my routes file.
scope(:path => '/classic')
But all that does is require a prefix for every URL. I'm really not sure how to go about this!
This is a route namespace. Take a look at this section in Rails Routing from the Outside In: http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing
namespace :classic do
# your routes here
end
This will do 3 things -
the path to the controller files need to be under /app/controllers/classic/
the name of the controllers need to change to Classic::ControllerName
the url is now /classic/controller/action
This sounds like what you want, but you can modify this to get just the parts you want if you don't want all 3.
In route.rb file:
#Of course, you have to configure the right http method.
get 'wills/new' => 'wills#new', as: 'to_classic_wills_new'
Hope this helps!
Just wanted to know, what does this line mean in the routes.rb file:
AppName::Application.routes.draw do
Please explain. I am new to Rails.
Have a read through this page.
Basically, within the block passed to Application.routes.draw (which is just a call to a method defined in ActionDispatch::Routing module within the Rails core framework), you define all the URLs/Paths that you want your Rails application to respond to.
You can see all these route definitions, by running:
rake routes
in your terminal.
It is the main routes file which defines the root and other paths for the link.
It is used as suppose you want to change your index page from default ruby on rails to your index page you make changes to file and add
root to: "controllername#index"
This file is also used to add the model to the application
resources: "model_name"
Apart from this you can also define links in your rails application
get 'courses/index'
So going from courses controller to view of the index.
I'm very new to RoR and I'm trying to get a very basic site going. I have my home page working okay, but when I try to add a new page, I keep getting "No route matches" in the log. Here is the output from rake routes:
Prefix Verb URI Pattern Controller#Action
inventing_index GET /inventing/index(.:format) inventing#index
ideas_index GET /ideas/index(.:format) ideas#index
root GET / ideas#index
However, when I go to mysite.com/inventing or mysite.com/inventing/index I get the no route matches error. mysite.com/ shows the app/views/ideas.erb as hoped. All I did was rails generate controller inventing index. Is there something else I have to do to activate the route?
I'm running ruby 2.0.0p247 and rails 4.0.0 with passenger/apache on centos 6. I installed all the ruby/rails/passenger stuff, so its possible something isn't setup properly.
Thanks
EDIT: Here is my routes.db file:
Rortest::Application.routes.draw do
get "inventing/index"
get "ideas/index"
root to: 'ideas#index'
end
tldr: Your problem is probably the route. Change get 'inventing/index' to get 'inventing/index'=> 'inventings#index' to correctly point the route to your controller action.
There are four things you need to do when adding a new page/route. I usually do them in this order for static pages:
1) Create a controller with the appropriate action for each page
You already did this with rails generate controller inventing index, but make sure that:
In your app/controllers folder, you do indeed have a file called inventings_controller.rb
In inventings_controller.rb, you have at least this:
class InventingsController < ApplicationController
def index
end
end
2) Create a view for each controller action
Make sure that:
In your app/views/inventings folder, you have a file called index.html.erb
Puts some simple HTML in that file, like <h1>Testing123</h1>, just so you can see if it's working.
3) Add a route for each controller action
It looks like this may be the problem. In your routes.rb file, you'll need this:
get 'inventing/index' => 'inventings#index'
Your root to works because it's actually pointing directly to your controller action, but rails isn't smart enough to guess that inventing/index should go to the index action in your inventing**s** controller.
4) As others have suggested, restart your rails app
You can do this with ctrl+c at the command line, then rails s again to start it back up.
I'm new in ruby on rails and I have a problem. When I starе server " rails server and going to the page
www.localhost:3000
in my browser everything works fine with ROR default page, but.... after making controller rails generate controller demo index
and going to the page
www.localhost:3000/demo/index , localhost:3000/demo - the same
- there is nothing, seems like page doesn't exist, only a blank page. I tried to change index.html.erb code
<h1>Hello World</h1>
- no change. I want to practice with ROR, but can't because of that bug.
Work environment: Windows 7 64-bit
Database: MySQL
You need to have a view corresponding to your action.
If your controller is called Demo and your action is index it should be
# app/controllers/demo_controller.rb
class DemoController < ApplicationController
def index
end
end
and the view file should be at app/views/demo/index.html.erb.
The index action is the base action of the controller. Try just:
http://localhost:3000/demo
to access the index.
By default routes /demo/index is the show action with the id of index.
make sure
views/layout/application.html.erb has yield and index.html.erb have some text
check your routes.rb
root :to => 'demo#index'
Go to http://localhost:3000/
Uncomment this line from your config/routes.rb file
match ':controller(/:action(/:id))(.:format)'
Remove index.html file from public/index.html
First of all delete or rename the index.html file from public folder.
Then set root :to => 'demo#index' at route file.
Then restart the server by using this command. rails s by default your server will run at port 3000.
Now simply type http://localhost:3000/ or http://localhost:3000/demo/index
Have you deleted
app_root
--public
----index.html
yet?
Rails will look in the public folder first for a matching file before it will goto build one from your views.
As indicated by João there, you will also need a view for that. Additionally, delete public/index.html and finally add a route for your controller.
If you had created a scaffold Rails start guide you would already have the route there for you.
I am new to Ruby on rails and working in a new test application. Consider my application url is : www.test.com
I would like to place a page by creating controller and respective views, the page should look like the below
www.test.com/articles/book-test-page
I created a controller "article" (defined the action as
def book_test_page
...
...
end
and folder under view as view/articles/book-test-page.
What routing information should i add in the routes.rb file to make this url bring up the specified url work? Any suggestion would be helpful..
Thanks,
Balan
get 'articles/book-test-page', :to => 'articles#book_test_page'