Rails 3 and SQLite - handling exceptions - ruby-on-rails-3

I'm currently building a portfolio with a basic custom content management system and I'm having a problem with SQLite errors.
As the majority of the pages on the site are created using the CMS they are stored in a table.
I want to implement a way of catching errors in the url and rendering a nice looking error page - rather than the standard Rails "Action Controller Exception" page.
I've had a thorough Google and really struggled to find anything resembling my problem. I'm quite new to RoR, so it's not something that is immediately obvious to me - if there is such a simple solution.
If someone could at least point me in the right direction, it would be most appreciated.
Many thanks

The Exception page with the stacktrace is shown only in development mode.
In production, when there is a routing error i.e. some error in the url as you mention above, it will render the public/404.html page so all you need to do is customize that page to get a nice looking error page.
To test it in development, in the config/development.rb file, change
config.consider_all_requests_local = true
to config.consider_all_requests_local = false

Related

Rails 5 routing: why is "redirect_to #model" different than "redirect_to model_path(#model.id)"?

I am working on a Rails 5 app and have a model called #offer. In the Offers controller I have an action where I want to redirect to a specific offer I got from the database (Offer Id = 14 in the examples below.)
Working in the development environment, if I use redirect_to offer_path(#offer.id) in the controller, the browser correctly displays the offer in the https://dev.example.com/offers/14 URL. Notice the dev part in the URL. So far so good.
However, if I use redirect_to #offer in the controller, the browser tries to open the https://example.com/offers/14 URL (that's the production URL) and the page shows an error (We're sorry, but something went wrong.
If you are the application owner check the logs for more information.)
I would like to use redirect_to #offer, but first, I think I need to understand why one redirect method behaves differently than the other. Thanks for any insight.
This question is old but still I am answering as it might help other rails users in the future.
In Offers controller redirect_to #offer or redirect_to offers_path(#offer) would resolve to the same path /offers/:id be it production or development.
I think, in the production database offer with id 14 does not exist and also in the controller, if op is using find method without rescuing exception then, the show action might be erroring while trying to fetch the Offer with id 14 from the production database but the find method my have returned exception which if not rescued the rails might show a default error response.
We don't have controller code posted by the op but to me, this seems to be the most logical answer.

Can't upload new product image backend prestashop

I need a little help here, i hope someone here can help me for a hint or clue.
First of all, I'm not programmer. I'm just web admin who can use cms and basic html.
I was using PrestaShop for my online shop. In the backend, I can't upload new product image anymore.
The error just blank without any sign for me. Here for the screenshot:
I appreciate it, if someone can help me, thanks, and sorry for my english.
#PanjiWiyono This errors don't really give us a quality information, but this is a start. In your JS code should be a ajax query that has an error when converting results to json (first error). You should check what's the exact error that this request is returning by inspecting in browser developer console.
If you detect that the second error is in fact, the response of this ajax query, well now we almost have the problem.
The second error should be related to data size. Check this: Error while sending QUERY packet
Anyway, you should check DbPDO.php class. You can use debug_backtrace function to display the complete stack, but definitly if error is related to a basic PHP class issue you will have not help knowing wich classes are in calling stack.
Good luck.
Simple solution
maybe a extra module is in a conflict with the prestashop core files. go to advanced parameters, performance and disable third party modules and try again.
other option is reupload admin folder with other name and
js folder check again
hope it will work for you

"__RequestVerificationToken is not present" error when I can clearly see it in Developer tools

I'm trying to a CSRF protection to an existing MVC4 web application which uses DevExpress grids. I've added the Html.AntiForgeryToken() into the forms on the aspx pages (which contain ascx as partials containing the grids) and can see the __RequestVerificationToken and it's value clearly in developer tools when a save is called.
I've tried commenting out all my ValidateAntiForgeryToken attributes bar one - I went with the delete post method for simplicity (And also to eliminate the DevExpress grids messing with it) and I still keep coming up against this error:
There was a HttpAntiForgeryException
Url: http://localhost:54653/Users/Delete/f86ad393-0039-44e8-beed-a66dbab9266e?ReturnURL=http%3A%2F%2Flocalhost%3A54653%2FUsers
The exception message is
The required anti-forgery form field "__RequestVerificationToken" is not present.
Does anyone have any idea why this might be happening? Could it be that the error is non-descriptive and it's actually that the token doesn't match rather than that it doesn't exist? In previous answers to this question people just say "oh, you have to add the token," which is obviously not helpful here.
Are you submitting the form manually through Ajax? If that's the case, you need to pass the anti forgery token as another parameter with the name "__RequestVerificationToken".
Point 1 : Make sure if your application is has https secure protocol. Please load in https.
Point 2 : In case of DevExpress you have to call in the below pattern.
ViewContext.Writer.Write(Html.AntiForgeryToken().ToHtmlString());
After struggling with this for days I had a thought - maybe the browser is stopping the cookie being written. I did a search for dev servers and cookies not being written, and found that with Chrome and IE10 and up that there's problems writing the cookies.
I downloaded Firefox and tried it with that and it worked instantly. I then reapplied all the validate attributes to the all the controller methods and the all worked, every single one of them! Even the DexExpress postbacks seem to be working correctly.
I'll carry out more exhaustive testing, but for now, I think we're there.
Not exactly. If MVC AntiForgeryToken is already defined on page where you are using MvcxGridView and you want to protect grid actions you should send this token back to server during grid client side begin callback event.
settings.ClientSideEvents.BeginCallback = "function(s,e) { e.customArgs[\"__RequestVerificationToken\"] = $('input[name=\"__RequestVerificationToken\"]', $(s.GetMainElement())).val(); }";

Sinatra Rspec - testing that a view has rendered

I am writing tests for a Sinatra app that takes input from an API via a gem. Once I have the API response I need to test that the template has correctly rendered. The response of the API will be the HTML of the page that I am loading.
My first instinct was to write a test that looks like this:
describe 'the root path'
it 'should render the index view' do
get '/'
expect(last_response).to render_template(:index)
end
end
Unfortunately when I try this I get the following error: undefined method `render_template'
I was wondering if anyone has encountered this problem - it seems like it should be an easy fix, but I can't seem to find any documentation to help with it.
I'm currently not testing views at all because of time constraints, but I did have some limited successs with Rack::Test.
In theory you can say:
require 'rack/test'
include Rack::Test::Methods
def app
Sinatra::Application
end
describe 'it should render the index view' do
get '/'
expect(last_response).to be_ok
expect(last_response.body).to eq(a_bunch_of_html_somehow)
end
If I were to go this road again, since my views are haml, I could implement the a_bunch_of_html_somehow method using a call to Haml::Engine -- but I'm not sure whether that helps you.
I'm lifting this wholesale from the Sinatra site here -- the page is well worth a read.
We ended up scrapping this approach since it was better handled by integration testing tool suites such as Selenium or Capybara. There is no equivalent that I could find in the basic Sinatra Rspec suite that could do this - it made more sense to move it into a different scope

How to do display security check authorization?

I am working around permission model in my project so when a particular user is not authorized then a screen is displayed depicting that the a user is not authorized to access the screen (which according to my best knowledge comes from a ArtifactExecutionFacadeImpl.groovy).
Is there any possible way to show that catch that exception and show it in a message.
So could you please suggest a way to do the same.
The current code returns a 401 (unauthorized) error when the authz fails (see MoquiServlet.groovy:68). You can display a custom screen with a code change instead of just returning the 401 error, or you can modify the web.xml file to specify a page to render for that particular response code (just like you can as part of the Servlet spec for any response code through the web.xml file).
Rendering a configured screen would be a useful enhancement to the framework (would be much cleaner and more flexible than either of the approaches mentioned above). I'll think on this and may work on it in the near future. If you or anyone end up building this, feel free to send over a pull request on GitHub and I'll take a look at it.