ActiveAdmin taking wrong http method for update and destroy actions - ruby-on-rails

Rails version - 5.2
Active admin version - 2.9.0
I have installed and configured active admin in my rails API application. Everything is working fine, except for the update, delete action of any controller, and logout of the admin user.
Here is my applicaiton.rb file
I have added method override in application.rb file though it is taking the POST request method for any update or delete request. It is working fine in my local even though it is taking POST request but when I deployed the code on the staging environment. I have found this thing. On my staging environment, that route is not present hence it is giving 404 error.
Below is the screenshot of the Update admin user request.
Can someone please help me to fix this issue?

I have finally fixed the issue. I am assuming the issue might be with my staging web server configuration otherwise it was working fine in my local in both the environments local and staging.
Post the answer here so it might help people in future.
By default the browser only supports for GET and POST requests. If we want to use any other request methods then we need to pass that request method in the parameter _method. You can read more about it here.
That wasn't happening in my case though i have added config.middleware.use Rack::MethodOverride in application.rb.
For resolving the issue, I have added the use Rack::MethodOverride in my config.ru file. It means before running the rails application it will use this method. I have added this code and that's it everything is working fine now.

Related

Rails params nulled when responding to csv format in production

I'm having a weird problem I cannot seem to figure out related to rails params.
I have a Controller where I permit any param. In this controller, a param (csv_type) controls which CSV file I will respond. CSV of type registration or attendance, for example.
This param is built in URL via path (e.g.: abcs_path(#abc, format: 'csv', csv_type: 'attendance') resulting in /abcs/id.csv?csv_type=attendance.
I would expect {"csv_type"=>"attendance", "controller"=>"abcs", "action"=>"show", "id"=>"45", "format"=>"csv"} but in production I get {"controller"=>"abcs", "action"=>"show", "id"=>"45", "format"=>"csv"}. csv_type is gone.
This works flawlessly locally and I get all params. Also works well on Heroku review apps. Also works well when I run locally on RAILS_ENV=production.
But in real production, it doesn't. It also doesn't work on staging which is configured with RAILS_ENV=production. Though review apps are also RAILS_ENV=production, which makes the whole thing a mystery.
Ideas:
- nginx messing up with domain (staging and production are on official domain, review app is Heroku domain, local is localhost).
- sqreen somehow intercepting this
- mime.types and https (have tried adding csv to rails, with no success). Didn't add on nginx
- some rails config on params permit (though I'm doing params.permit!) on before_action just to make sure
- some ENV variable messing up?
- can't think of anything else ???
Thanks in advance for the help!

405 not allowed nginx, CORS? Nginx config? Or something else?

Working on an app built using Ember.js and Rails.
Authentication is being done with ember-simple-auth using Oauth2.
I am able to login to my app locally in my development environment, but as soon as I try to login on my production server (through Heroku) I start receiving a nginx 405 not allowed status code.
First thing I thought was maybe it is my request headers / CORS. I am using rack-cors gem on my rails side and configured it based directly off the readme example. Here is my application.rb
Researching, I found the same problem with the solution being to configure Nginx side of things, but I figured since that is being handled by heroku I wasn't really sure if that was where I need to make my changes.
Let me know if there are any other files/info that could help.
What is the best way to debug this problem?
Try using this first to rule out CORS:
https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi
Does the route work locally when you use Postman or a similar tool?
Are you requesting HTML or JSON?

Redirection in RubyOnRails

I just got an SSL certificate for "example.com". After making some configurations in NGINX I could finally redirect "http://www.example.com" calls and "http://example.com" calls to "https://example.com". Everything works fine, but when I type "https://www.example.com" an SSL error is shown. Is there a way to redirect "https://www.example.com" calls to "https://example.com" using some Ruby code?
Handling the redirect at the Rack level and not inside of the Rails app is my preferred way to solve this requirement.
https://github.com/jtrupiano/rack-rewrite#scheme
The link takes you to the part of the README that addresses this question. I've used this gem in the past and it works really well.

can't log on to wp-admin when wordpress is hosted as a rails subdirectory

I have a rails app on heroku, and a wordpress-heroku install also on heroku. I'm using the rack-reverse-proxy gem to redirect my wordpress to the /blog directory on my rails app. I followed all the instructions here:
http://rywalker.com/setting-up-a-wordpress-blog-on-heroku-as-a-subdirectory-of-a-rails-app-also-hosted-on-heroku
When I access my wordpress blog on its normal address, everything works fine. However, when I have it set up under a subdirectory of my rails app, I can't log in. I go to wp-login.php I enter my credentials, get forwarded to /blog/wp-admin.php, and then immediately I am redirected back to /blog/wp-login.php?redirect_to=http%3A%2F%2Flocalhost%3A3000%2Fwp-admin%2F&reauth=1. The only cookie that gets set is the wordpress_test_cookie, but none of the other wordpress cookies make any appearance in my browser.
I have tried many things to fix this including using rack-reverse-proxy to forward all rails traffic to wordpress, so I don't have to use a /blog subdirectory in case that was causing the problem. But the exact same behavior results. I've also determined that the :preserve_host setting in the Rack::ReverseProxy config doesn't seem to make any difference whether it's true or false.
Ideas?
A) What do you have in your wp-config.php? Should be something like this:
define('WP_SITEURL', 'http://www.DOMAIN.com/blog');
define('WP_HOME', 'http://www.DOMAIN.com/blog');
B) In the Rack::ReverseProxy settings, are you pointing to the blog (wordpress) herokuapp.com URL, not the main site (rails) URL, right? I realize my post isn't clear on that point.
I finally tracked this down to a bug in rack-reverse-proxy. The set-cookie header was being sent in an improper format, so only the first cookie was being interpreted correctly by the browser. That happened to be the wordpress test cookie. All the other (useful) ones were being thrown away, so of course I could not log in.
I plan to submit a bug and branch to rack-reverse-proxy but in the meantime I fixed it with this patch in my config.ru:
class MyReverseProxy < Rack::ReverseProxy
private
def create_response_headers(http_response)
response_headers = super(http_response)
if response_headers
if response_headers["Set-Cookie"].is_a?(Array)
response_headers["Set-Cookie"] = response_headers["Set-Cookie"].join("\n")
end
end
response_headers
end
end
# this is to make /blog show my wordpress blog
use MyReverseProxy do
reverse_proxy_options :preserve_host => false
reverse_proxy(/^\/blog(\/.*)$/, 'http://your-blog-server.com$1')
end

Sending mail from Rails works at console but not in my application...?

I have a pretty simple Rails app and I want send an e-mail when a work request is created. I'm pretty new to Rails so I found some details on how to do it online. Set up a Mailer, configured it, etc. Set up my templates. Fine.
In my work_requests_controller.rb I have:
def create
#work_request = WorkRequest.new(params[:work_request])
respond_to do |format|
if #work_request.save
# Tell the mailer to send a welcome Email after save
PersonMailer.work_request_init_email(#work_request).deliver
format.html... etc.
In know mailing is working because if I go to the Rails console, create WorkRequest object, and use that exact same line (PersonMailer.work...) it sends the mail just fine. But when I create a work request in my application no mail is received, no error is displayed in the browser or in logs/development.log.
In the server output I see the HTML version was rendered, and I see the info about the e-mail and it all looks both hunky and dory. Since I get no errors I'm at a loss as to how to proceed.
OK, I am officially an idiot. :-)
Working on a different problem, I was editing application.rb. I figured I needed to restart the server to make it see those changes. Suddenly, the e-mails work from inside the app.
D'oh! I did not realize (rookie mistake) that I needed to restart the server for the app to see the e-mail configuration I had placed in environment.rb yesterday. I never tried that for some reason.
I see now that and other components are run only when the server starts up. So of course, when I started up console of course it runs all the initializers and so the e-mail configuration was visible to it and the e-mails were sent.
So the answer is, restart your server, stupid. Well, anyway, at least it's working now...I'll take it where I can get it!
I would start with moving your email command to your WorkRequest model as a 'after_create' action
after_create :send_init_email
def send_init_email
PersonMailer.deliver_work_request_init_email(self)
end
See if that works, or aleast gives you a better error msg.

Resources