Rails - Create internal server error (500) at localhost - ruby-on-rails

I couldn't see 500 error when I accessed a URL for which there is no Data, rather it was showing the 'Template missing' error. At the same time, when I ran it at the server, it had shown the 500 error. I need to do testing at local machine. So please tell me how I can create such a situation at localhost?
Thanks & Regards,
Rajesh

You can create such situation in localhost if you run the server in production mode: rails s -e production (Of course, if the error is still present)

If you are getting a template missing error is most probably because you are missing the view file for a given controller action
Ex: if you have a controller called users
class UsersController < ApplicatationController
def index
end
end
by default rails is expecting a view in
app/views/users/index.html.erb (or haml)
But if you could post the error log you are getting we might be able to help you more
regards
sameera

It's a simple problem with your corresponding view not being present. Open the control file which corresponds to your url. Then see the action which is being called and then, see if the corresponding view is available in the app/views/ folder.
The reason for 500 error is the same 500 says that there was an internal error at the server side.
Also, don't go about changing the url's charecter and stuff. IT WONT WORK!

Related

Getting "Uninitialized constant" routing error when loading a view

I'm a beginner to both ruby and rails, and using Rails 5.17 to develop a web app for a class.
Creating the empty Rails project was successful, but something is going wrong when creating a new controller. I generated a new controller named cars from the root of the project, which was successful. There was a file in app/controllers named cars_controller.rb which looks like this:
class CarsController < ApplicationController
end
I added a method to this file named hello that does nothing.
I then created a file named cars.html.erb in the app/views/layouts directory. This file is a basic page of html code.
In config/routes.rb, I added the following:
get '/cars', to:: 'cars_controller#hello'
resources: cars
After all of this, I ran rails server, and opened localhost:3000 in a browser. This brings up the normal Ruby on Rails welcome page.
But when I go to localhost:3000/cars, I get the following:
Routing Error
uninitialized constant CarsControllerController
I've tried changing the name of the cars_controller.rb file. I've tried changing the name of the class in the controller file from CarsController to Cars. I've tried many different routes in routes.rb. I finally tried uninstalling Rails 5.17 and installing Rails 5.13.
I'm very confused, and I'd be grateful for any advice I can get. Thanks in advance!
One of the great things about Rails is its preference for convention over configuration. However, for this to really benefit you, you need to stick to doing things “The Rails Way” rather than your own way, wherever possible.
In this case, start by getting rid of your custom get route, and just use resources :cars.
From the command line, run rake routes (you might be able to run rails routes on your rails version too) and see the routes that it has created for you.
Now, rename the method you added to your CarsController from hello to index.
Move your hello.html.erb file from app/views/layout to app/views/cars/index.html.erb.
Finally, start the rails server (rails start) and load the url http://localhost:3000/cars in your browser.
—-
Note that templates in app/views/layout have a special purpose. These are used to apply a general template to your views. Look up the use of layout within a controller for more details
I think you have an error in how you had defined your route - you don't need _controller.
Instead, try this:
get '/cars', to: 'cars#hello'
Also, keep in mind that in your cars directory you need the view: hello.html.erb

Do I need to generate a controller for a Ruby on Rails Controller call?

I'm setting a session variable to a user's geographical state. I have to use a session variable because I run code on the server specific to that user on page load and I need to know where they are. This code is set up to just update the session variable.
states_controller.rb
class StatesController < ApplicationController
def loc
session[:location] = params[:location]
end
end
routes.rb
post "states/loc" => "states#loc"
The code routes properly and the session variable is updated.
However, when the process is complete I get a 500 error in the console "Missing Template" in the views directory. I haven't seen any tutorials tell users to call the command "rails generate controller" and I'm in the unique situation where I can't call this command.
What possible side effect are there to ignoring this 500 error?
*I'm running an older version of ruby and rails.
What possible side effect are there to ignoring this 500 error?
Each request is crashing your rails server. Thats not good. Since it means that some cases it may have to restart after every failed request - that eats resources like Homer Simpson at a buffet.
Your app should not be raising uncaught exceptions that cause 500 errors - thats just decent professional practice.
So how do I fix it?
Simple, if you don't want the default behavior of rendering a view tell rails to do something else:
class StatesController < ApplicationController
def loc
session[:location] = params[:location]
head :created
end
end
This sends an empty response with the 201 - CREATED http header.
See Rails Guides - Layouts and Rendering in Rails

InvalidCrossOriginRequest when trying to send a Javascript Asset

I'm trying to create an "asset controller" shim which will filter static asset requests so only authorized users can get retrieve certain assets. I wanted to continue to use the asset pipeline so I setup a route like this
get 'assets/*assetfile' => 'assets#sendfile'
Then I created an AssetsController with one method "sendfile". Stripping it down to only the stuff that matters, it looks like this:
class AssetsController < ApplicationController
def sendfile
# Basically the following function forces the file
# path to be Rails.root/public/assets/basename
assetfilename=sanitize_filename(params[:assetfile] + '.' + params[:format])
send_file(assetfilename)
end
end
It looks like I have to run this in production mode as rails by-passes my route for assets in development. So I precompile my assets and I can verify in the controller that the files exist where they are expected to be.
However, now the problem is that I'm getting a "ActionController::InvalidCrossOriginRequest" when the Javascript asset is requested (just using the default application.* assets for now). I've read about this error and I understand that as of Rails 4.1 there are special cross-origin protections for Javascript assets. Sounds good to me, but I don't understand where the "cross-origin" part is coming from. Using firebug, I can see that the asset requests are being requested from the same domain as the original page.
I am certain that this is the problem because I can solve it by putting "skip_before_action :verify_authenticity_token" in the beginning of my controller. However, I really don't want to do this (I don't fully understand why this check is necessary, but I'm sure there are very good reasons).
The application.html.erb file is unchanged from the default generated file so I assume it's sending the CSRF token when the request is made, just as it would if I didn't have my own controller for assets.
So what am I missing?
Ok, I think I answered my own question (unsatisfactorily). Again, long post, so bear with me. I mistakenly forgot to add this to my original questions, but I'm using Ruby 2.2.0 and Rails 4.2.4.
From looking at the code in "actionpack-4.2.4/lib/action_controller/metal/request_forgery_protection.rb", it looks like Rails is doing two checks. The first check is the "verify_authenticity_token" method which does the expected validation of the authenticity token for POST requests. For GET requests, it ALSO sets a flag which causes a second check on the formed computed response to the request.
The check on the response simply says that if the request was NOT an XHR (AJAX) request AND the MIME Type of the response is "text/javascript", then raise an "ActionController::InvalidCrossOriginRequest", which was the error I was getting.
I verified this by setting the type to "application/javascript" for ".js" files in "send_file". Here's the code:
if request.format.js?
send_file(assetfilename, type: 'application/javascript')
else
send_file(assetfilename)
end
I can skip the response check all together by just adding the following line to the top of my controller class:
skip_after_action :verify_same_origin_request
The check on the response seems pretty weak to me and it's not clear how this really provides further protection against CSRF. But I'll post that in another question.

FTP authentification on Rails

I am trying to log into a ftp account with rails, but the authentification fails and i don't know why. My html page calls a method :
test(site.host,site.ftp_user,site.ftp_pw)
My helper defines this method :
def test(host_ip,user,pass)
ftp = Net::FTP.new(host_ip)
ftp.login('user','pass')
ftp.system
ftp.close
end
The information is working with FileZilla so the problem is somewhere else. Any idea ?
I'm getting confused with what should be between quotes or not. I mean site.ftp_user and site.ftp_pw are strings so i don't know why i have to use quotes. But if i don't use them, i get a gettaddrinfo error...
Here is the SocketError i get when removing the quotes :
getaddrinfo: Name or service not known
Kinda lost here :/
Yeah, you want to remove the quote for user and password in your helper.
Then, what line is the getaddrinfo error on? maybe you can share it here.
Is it on the ftp.system line...if so it looks like system may not be the method call that you want.
If you want to open a file or something checkout the examples here http://stdlib.rubyonrails.org/libdoc/net/ftp/rdoc/classes/Net/FTP.html

404 Custom Error Pages in Rails 2.3

I'm overriding render_optional_error_file to render a custom page whenever there's an error. This works great if there's an error within the application, it renders "shared/error.erb" without problem.
My application controller has a few before_filters which are responsible for setting the colour scheme of the page, defining the menu items, and authenticating users. These also run when there's an application error, which is desired.
However when a 404 page for a file is rendered, none of these filters run, so I get a black page with no menu. Is there a way I can trigger these to run? And are there any reasons why I shouldn't do this?
When Rails encounters a missing file, it runs render_optional_error_file(404) with status 404 on the application controller, but skips all filters, presumably since an error has already occurred.
I added a method called run_filters to my application controller and then call that from render_optional_error_file:
def run_filters
#run filters or whatever
end
def render_optional_error_file(status)
run_filters
render "shared/error", :status => status
end
You can also test this behaviour on your development server by including the following in your application controller:
alias_method :rescue_action_locally, :rescue_action_in_public
Well, you may want to consider to create a special layout for your error pages. After all, it's a common practice to make error pages clearly distinguishable from normal ones.
But I don't understand why your before filters defined in application controller don't trigger. They really should trigger before any error happens in your actions.
Could you provide us with some code from your application controller?

Resources