Why when I write rails generate controller home index the index.html.erb file is not created in the views folder? And when I run rails server and want to watch http://127.0.0.1:3000/home/index terminal output.
Started GET "/home/index" for 127.0.0.1 at 2021-01-10 01:54:30 +0200
Processing by HomeController#index as HTML
Completed 204 No Content in 0ms (ActiveRecord: 0.0ms | Allocations: 60)
When I check error log I see HomeController#index, I would like you to check rails routes first. You should see a result similar to this:
Prefix Verb URI Pattern Controller#Action
home_index GET /home/index(.:format) home#index
...
If you are seeing homecontroller#index instead of home#index you can access by going to related URI pattern.
You can control which files are created when you run rails generate command. This is a sample output:
➜ rails g controller home index
Running via Spring preloader in process 1934
create app/controllers/home_controller.rb
route get 'home/index'
invoke erb
create app/views/home
create app/views/home/index.html.erb
invoke helper
create app/helpers/home_helper.rb
invoke assets
invoke scss
create app/assets/stylesheets/home.scss
As you see there is a file app/views/home/index.html.erb make sure that it is not empty. For example put <p>Home</p> in this file, if it is empty.
I hope it is clear enough and I think this will solve your problem.
Related
I am working on a page that shows user-uploaded videos and pictures. The path for this page is /users/:id/ and the pictures/videos are located in /public/media/users/[userName]/
user_controller:
def show
#user = User.find(params[:id])
posts = #author.posts
#pictures = posts.select(&:picture?)
#videos = posts.select(&:video?)
end
views/user/show.html.erb:
<% #pictures.each do |picture| %>
<%= image_tag("/" + picture.path) %>
<% end %>
<% #videos.each do |video| %>
<%= video_tag("/" + video.path) %>
<% end %>
The page loads all of the pictures/videos correctly. For example, one of the generated video tags looks like <video src="/media/users/user1/example.mp4"></video> which is perfect.
However, after loading the page Rails seems to issue an extra GET request for each of the videos.
Completed 200 OK in 126ms (Views: 13.7ms | ActiveRecord: 1.9ms | Allocations: 21392)
Started GET "/users//media/users/user1/vids/example.mp4" for ::1 at 2022-08-13 10:21:25 -0400
Started GET "/users//media/users/user1/vids/example2.mp4" for ::1 at 2022-08-13 10:21:25 -0400
Started GET "/users//media/users/user1/vids/example3.mp4" for ::1 at 2022-08-13 10:21:25 -0400
ActionController::RoutingError (No route matches [GET] "/users/media/users/user1/vids/example.mp4"):
ActionController::RoutingError (No route matches [GET] "/users/media/users/user1/vids/example2.mp4"):
ActionController::RoutingError (No route matches [GET] "/users/media/users/user1/vids/example3.mp4"):
Some observations:
This is only occurring for videos, no extra requests are generated for pictures
Rails seems to be prepending the controller name to the start of these paths
Why are these requests being issued?
First, I have no idea why the requests are happening twice, I suspect Rails is either trying to speed up page loads by delaying loading the actual videos until after the page has rendered or it is making the second request to try and get a :poster value from the video object (see docs for video_tag)
But, I think this is probably intended behavior and the problem isn't that the 2nd GET request is happening, the problem is that the 2nd GET request has a bad URL.
I think I have an idea why the 2nd call's URL is bad.
My thought is that Rails doesn't have all the [userName] folders added to the asset pipeline.
Hence why you need to have the '/' before the picture and video path.
But, because the folders aren't in the pipeline, they are being resolved by just appending the generated video route to the current route, hence why you are seeing two backslashes together in the 2nd GET calls.
IF these folders are dynamically created, adding them to the asset pipeline without restarting the app is beyond me, but probably possible with some meta Rails programming.
If these folders are static, or the app can be restarted after a new folder is created, you can make sure they get added to the asset pipeline:
First, see if these folders are in the asset pipeline:
You can view the search path by inspecting Rails.application.config.assets.paths in the Rails console.
If they aren't in there, it might be because you've put them in /public but haven't enabled public assets:
In previous versions of Rails, all assets were located in subdirectories of public such as images, javascripts and stylesheets. With the asset pipeline, the preferred location for these assets is now the app/assets directory. Files in this directory are served by the Sprockets middleware.
Assets can still be placed in the public hierarchy. Any assets under public will be served as static files by the application or web server when config.public_file_server.enabled is set to true. You should use app/assets for files that must undergo some pre-processing before they are served.
# application.rb
module YourAppName
class Application < Rails::Application
...
config.public_file_server.enabled = true
...
end
end
or this behavior can be customized per environment:
# production.rb, development.rb, or test.rb
Rails.application.configure do
...
config.public_file_server.enabled = true
...
end
If this still doesn't fix it, you might have to manually add public/media/users/* to the pipeline like this.
I am learning how to login from Rails and I wanted to know one thing:
I have several files which I want to show when a certain condition is met, in this case logging in will redirect me to another file called starter.html.erb
I am trying to redirect it through both the controller and the routes files and I get the following error:
No route matches [GET] "/app/views/usuarios/starter.html.erb"
Can you please tell me what I'm doing wrong? Thanks!
Controller portion:
redirect_to search_starter_path
routes.rb portion:
get "/search/starter" => redirect("/app/views/usuarios/starter.html.erb")
You have to use the controller#action syntax to redirect. Example: if your controller is usuarios_controller.rb you should have inside an action named starter. Then in your routes.rb put this entry:
get "search/starter" => 'usuarios#starter'
I scoured my app's directories, and I can't find the html page for the default rails Welcome Aboard page. I also cannot find a route for the default Welcome Aboard page in routes.rb. How does my rails app route http://localhost:3000/ to a non-existent page in my app?
The rails server produces this information:
Started GET "/" for 127.0.0.1 at 2013-07-31 02:00:13 -0600
Processing by Rails::WelcomeController#index as HTML
Rendered /Users/7stud/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/railties-4.0.0/lib/rails/templates/rails/welcome/index.html.erb (0.1ms)
Completed 200 OK in 3ms (Views: 2.5ms | ActiveRecord: 0.0ms)
So it looks to me like there is a controller buried in a gem somewhere that handles the request.
Since Rails 4, the "Welcome aboard" page is no longer located in public/index.html. It is - as you've already detected - located inside one of the Rails gems.
So you already answered the question yourself; the "Welcome aboard" page is - in your case - located at /Users/7stud/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/railties-4.0.0/lib/rails/templates/rails/welcome/index.html.erb
To get rid of it, following the instructions on the page. Basically they are:
Create a controller
Add a root route in config/routes.rb to route to that newly created controller.
As for how the request to your application ends up at a controller inside railties, let's dig into the gem: Inside Rails::Application::Finisher we find this:
initializer :add_builtin_route do |app|
if Rails.env.development?
app.routes.append do
get '/rails/info/properties' => "rails/info#properties"
get '/rails/info/routes' => "rails/info#routes"
get '/rails/info' => "rails/info#index"
get '/' => "rails/welcome#index"
end
end
end
This block adds a few routes to your application when running in development mode - one of those is the route to the "Welcome aboard" action: get '/' => "rails/welcome#index"
This - like any other initializer - is done when your start your application server (running rails server or however you do it). In the case of Finisher, all its initializer are run after all other initializers are run.
Note how the routes are appended so that they are appear last in the Routeset. This, combined with the fact that Rails uses the first matching route it finds, ensures those default routes will only get used if no other route is defined.
On visiting localhost:3000 I'm getting the typical:
No route matches [GET] "/"
I don't want to see that error screen. I already trying with localhost:3000/passbook/v1/passes/ but still nothing so I typed:
rake routes
and got this output:
passbook GET /passbook/v1/passes/:pass_type_identifier/:serial_number(.:format) passbook/passes#show {:pass_type_identifier=>/([\w\d]\.?)+/}
GET /passbook/v1/devices/:device_library_identifier/registrations/:pass_type_identifier(.:format) passbook/registrations#index {:pass_type_identifier=>/([\w\d]\.?)+/}
POST /passbook/v1/devices/:device_library_identifier/registrations/:pass_type_identifier/:serial_number(.:format) passbook/registrations#create {:pass_type_identifier=>/([\w\d]\.?)+/}
DELETE /passbook/v1/devices/:device_library_identifier/registrations/:pass_type_identifier/:serial_number(.:format) passbook/registrations#destroy {:pass_type_identifier=>/([\w\d]\.?)+/}
passbook_log POST /passbook/v1/log(.:format) passbook/logs#create
How can I resolve this?
You haven't told Rails yet what your root path is. You can do this by saying
root to: "passes#show"
in config/routes.rb. Afterwards, you should see this in the rake routes output:
root / passes#show
Then it should work!
Additional tip
This may be over the top for your question, but following the test driven approach you should write a spec first:
describe "custom routing" do
it "shows the root page" do
get("/").should route_to("passes#show")
end
end
Use RSpec for this. Run the test in your console before having written the route itself by doing $ rspec and see the test fail at the right place.
Then, implement the routes above, run the test again—it should work. This way you ensure that you have written just enough code to meet your requirements, and that it is really the code you have written that lets you access the root path.
Is this a rails app you inherited from someone else?
Given the rake routes output you have provided, a url like localhost:3000/passbook/v1/passes/<EXAMPLE_PASS_TYPE_IDENTIFIER>/<EXAMPLE_SERIAL_NUMBER> should work based on the rule in the first line of the output, provided you replace EXAMPLE_PASS_TYPE_IDENTIFIER and EXAMPLE_SERIAL_NUMBER with valid values from the database.
The error message No route matches [GET] "/ for localhost:3000 is because there is no mapping for root in rake routes output.
You could modify the config/routes.rb file and add the following line at the bottom of the file:
root :to => "<controller_name>#<action_name>"
Eg:
root :to => "passbook/index"
if there is a passbooks_contoller.rb with a index method.
Best to learn more about rails routing from the documentation. Even better go through the Rails Tutorial to get a good understanding of the rails framework.
i'm trying to use the jQuery Uploadify on a Ruby on Rails project. i am able to browse for a file, and select it. the upload progress goes till 100% and then i get a HTTP error. my development.log is below
Processing ApplicationController#index (for 127.0.0.1 at 2010-02-07 18:33:01) [POST]
Parameters: {"Filename"=>"file.psd", "folder"=>"/uploads", "Upload"=>"Submit Query", "Filedata"=>#<File:/var/folders/j5/j5kRE9LqGzqgPWZPtCoi1k+++TI/-Tmp-/RackMultipart20100207-1470-c8y8uc-0>}
ActionController::RoutingError (No route matches "/javascripts/uploadify.php" with {:method=>:post}):
Rendering rescues/layout (not_found)
Anyone knows what's wrong here?
I believe your form action is very wrong - you're using Ruby On Rails, but action submits form to php file.
My guess is you probably have the default catchall routes in your routes.rb file:
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
Remove those lines and it should work.
See this excellent guide for all you need to know about Rails Routes.