I am new to rails and want to do a simple routing to root operation. My file path is app/views/slots/index.html.erb. When I go in my routes.rb file and see:
You can have the root of your site routed with "root"
just remember to delete public/index.html.
root :to => 'welcome#index'
I change the 'welcome#index' to "slots#index', I get this error from localhost:3000:
No route matches [GET] "/"
In the terminal, I use ctrl c to exit server and $ rails server to restart and still get the error. I watched my instructor do the same exact simple steps yet I get this error. Anyone know what I did wrong?
Edit : my bad, indeed the error is not corresponding to the solution I described below.
It seems you didn't create the slots controller.
You have to create a controller to display files. app/views/slots/index.html.erb is just a view, corresponding to an action of a controller.
So create the file app/controller/slots.rb
class SlotsController < ApplicationController
def index
end
end
You can do this faster with generator. In your terminal, cd to your application path and then
rails generate controller Slots index
To learn Rails I recommend you some reading here : http://guides.rubyonrails.org/getting_started.html
Related
I have a Rails 3.2 application with numerous resources. How can I change the routing so that every path has an extra directory prepended it? Obvioulsy I know how to do this on a resource by resource basis, but how do I do it to all routes?
So for example:
users
users/1
resources/new
resources/77/edit
assets/66
Would become:
cms/users
cms/users/1
cms/resources/new
cms/resources/77/edit
cms/assets/66
Please check the documentation. Basically, you need:
namespace :cms do
#all your resources
end
I regularly imports some tables from the production to development environment. One table is related to images, and when I run development, these images all causes ActionController::RoutingError (No route matches [GET] "/uploads/xxx.jpg"):.
The listing pages show lots of these images, which flooded my log. I am wondering if one can skip the logging of these routing error (for missing images only) in the development environment?
I have tried using rescue_from but it does not seem to work, and it is not fine-grain enough.
The new configuration level changes are not applied until server is restarted. Restarting the server should apply new routes. Also share the result of $ rake routes in terminal.
this is my first time posting to stackoverflow. I am learning Ruby on Rails and i want to ask the reason why my app doesn't redirect to the root i specified in my routes.rb.
I already deleted the public/index.html and also removed it from git.
Jba::Application.routes.draw do
get "home/index"
root :to => 'home#index'
end
Thanks for helping. :)
Make sure you clear the cache on your local browser in order to view the changes you've made to your code.
I have several custom routes in my rails 3.0 app, the simplest one of which is.
match "*user", "profile#index", :via => :get
Because of that route physical locations on the server are killed. As an example.
/images/rails.png
tries to route to the images user.
I also have to be able to setup where people access
/<username>/archive.zip
So
/buddy/archive.zip
Where the archive.zip is a physical file on the server that has been generated and put there. How can I achieve this in my routing.
For the later I have an actual folder structure in a root folder for /<username>/archive.zip so I was thinking somem sort of symlink would be easy, but without being able to hit physical locations on the server. I am kind of stuck/confused.
Any help is appreciated.
You probably want to have any static assets get handled by your web server before hitting your rails stack. This is generally done by setting the document root in your webserver to the public/ directory within your rails app to serve your static images/css/js.
This is greatly preferred over allowing Rails to serve static assets because web servers are much faster at handling these sorts of requests, and your not tying up your rails processes for these requests, which are often limited to less than a handful.
I am trying to use rack offline in rials to make my webpage available offline. By default rack offline takes all files from the public folder into the cache manifest. In which file should I make changes so that it will take the add the file that I want into the cache manifest. I want to include the file in my views folder.
You need to add it to your routes.rb file. Here is my routes.rb file with a customized manifest. This will give you the index and the new routes as well as all of the html files in your public root (*.html) and every file in a sub-folder to public (*/*.*). You can slice and dice that however you need it for stuff in the public folder.
I don't know how to get the database specific routes like show and edit while offline. I would imagine Javascript is needed. Check out Railscast episode 248 for some ideas for integrating JS
OfflineConfirm::Application.routes.draw do
#match '/application.manifest' => Rails::Offline
resources :contacts
offline = Rack::Offline.configure do
cache ["contacts/new", "contacts"]
public_path = Rails.root.join("public")
Dir[public_path.join("*.html"),
public_path.join("*/*.*")].each do |file|
p = Pathname.new(file)
cache p.relative_path_from(public_path)
end
network "/"
end
match '/application.manifest' => offline
end
The routes file above will produce the following application.manifest
CACHE MANIFEST
# 700ae3e3002382cb98b93c299d7b7bda151183b4703ef65d4c46b0ecf9c46093
contacts/new
contacts
404.html
422.html
500.html
index.html
images/rails.png
javascripts/application.js
javascripts/jquery.js
javascripts/jquery.min.js
javascripts/rails.js
stylesheets/scaffold.css
NETWORK:
/
None of the files in your views folder are available without a server. You want to make a route available in the cache manifest? For instance "/about", which corresponds to a "views/about.haml" file for instance?
Add this to your config:
offline = Rack::Offline.configure do
cache "about" # or whatever your route is
public_path = Rails.public_path
Dir[public_path.join("javascripts/*.js")].each do |file|
cache file.relative_path_from(public_path)
end
end