I've created a basic RESTful service with rails g scaffold and it fails to accept XML request sent by ActiveResource (works fine with JSON POST requests and XML GET requests). It fails with ActionController::ParameterMissing so obviously it has something to do with whitelisting but I can't figure out what exactly I should send. Any suggestions?
It seems that XML support was removed in Rails 4 so actionpack-xml_parser gem is required to deal with XML requests.
Add to Gemfile
gem "actionpack-xml_parser"
execute bundle install and configure the rack middleware by adding the following line to config/application.rb.
config.middleware.insert_after ActionDispatch::ParamsParser, ActionDispatch::XmlParamsParser
As per instruction from sikachu/actionpack-xml_parser
nJoy!
Related
I googled a little bit to find a proper and easy to implement way to generate Swagger documentation for an existing Rails API app. To be short, there are 2 ways of implementation: either via controllers, models or via Rspec controller/request specs. The list is not too long and the choice is not so easy to make:
swagger-docs gem, - the oldest one. Pros: implementation is to be
done in controllers. Cons: does not support Swagger V2 version
and limited to 1.2 only.
swagger-blocks gem, - improved version of swagger-docs, supports Swagger V2 version, implementation is to be done in controllers, models. But I couldn't make it work, Swagger Editor could not parse the generated json file.
rswag gem: extends rspec-rails "request specs" with a Swagger-based DSL. Personally, I found it really difficult to implement in an existing Rails app for 2 reasons:
you will have to modify existing request specs
request specs look really weird and the syntax is not RSpec-ish.
rspec-rails-swagger gem: implementation via request specs. The same cons as above.
Does anybody know other gems to generate Swagger documentation for an existing Rails API app ? Any suggestions are welcome ! Thank you.
The solution I came to is as to use rswag gem and rspec-rails-swagger gem
- install rswag gem by adding the following in your Gemfile:
#Gemfile
gem 'rswag-api'
gem 'rswag-ui'
group :development, :test do
gem 'rspec-rails', '~> 3.8.1'
gem 'rspec-rails-swagger', '~> 0.1.5'
...
end
run `bundle install``
run rails g rswag:install to generate swagger_helper.rb
to create a new request spec with rspec-rails-swagger, run rails generate rspec:swagger PostsController(adapt the name to your won controller you want to write the spec).
write some specs as explained in rspec-rails-swagger README
run bundle exec rake swagger to generate a swagger.json file.
mount RSwag API and RSwag UI engines by adding the following lines to your routes.rb file:
#../config/routes.rb
Rails.application.routes.draw do
mount Rswag::Ui::Engine => '/api-docs'
mount Rswag::Api::Engine => '/api-docs'
...#other routes come here
end
start up your rails server with rails s
navigate to localhost:3000/api-docs to see the generated Swagger documentation.
Note: it works pretty good and replies to the client requirements, i.e.:
generate Swagger 2.0 compatible documentation
be able to execute requests directly from Swagger interface to see the real data
I removed rswag-specs gem from Gemfile because it could not validate response schema returned in JSON API format by active_model_serializers gem I use in my Rails API app. I had always to:
generate the docs
comment out failing tests
then uncomment failing tests in case I need to generate some new documentation, that was really hard to maintain.
Now request specs are validated by RSpec and rspec-rails-swagger at the same time without hassle.
Hope this helps.
Our working solution is to use:
swagger-blocks gem to generate the Swagger JSON
Swagger UI Console Chrome
Extension to provide the Swagger UI
I'm trying to get a better handle on Rack middleware, and I've just learned that you can view all of the installed middlewares for a Rails app using rails middleware. In a fresh Rails app, that's about 25 different middlewares.
I'd really like to examine the source code of these different middlewares, but I can't find out where they live in the app. Have they all been compiled to executable, so the only way I can read the source is on Github? If so, how can I find those repos? Thanks!
For the ActionDispatch, ActiveRecord, Rails, ActiveSupport, and Testing middlewares, they'll be in rails/rails, the Rack stuff will be in rack/rack, and WebConsole will be in rails/web-console
They're not compiled into an executable, no. Each gem is downloaded based on the contents of your Gemfile and gets semi-magically loaded via your environment and config.ru.
I'm building an API with Grape on Rails 4.2. Here's a link to the repo on GitHub.
On the frontend, I have a JavaScript application built with EmberJS. Here's the link to the repo on GitHub as well.
I have updated the following gems so that I can format the responses of my API following JSON API standard, which appears to be required to update to Ember 1.13, then to Ember 2.0.
gem "active_model_serializers", '0.10.0.rc2'
gem "grape"
gem "grape-active_model_serializers", :git => 'https://github.com/jrhe/grape-active_model_serializers.git'
After that, I've got the following error when I call the API: IOError: Not opened for reading
According to the information I gathered here and there, I suspect this is a conflict with how I format JSON.
In default.rb, the file that inherit all API controllers, when I comment this line:
formatter :json, Grape::Formatter::ActiveModelSerializers
I don't have any error anymore, but obviously the response isn't serialized.
My questions are:
Do you have any idea how I can solve this?
Do you think it's relevant to use Grape to build the API in my Rails app? With Rails 5 coming, isn't that better to user rails-api and rewrite the whole app? I mean, I'm starting, it's maybe the right time to do that...
Let me know if you need more information.
Thanks in advance for your help.
I ended up rewriting the whole API using jsonapi-resources.
Using Rails 3 I want to use an X.509 certificate to sign parts of emails. There is a currently existing answer for Rails 2 at How do I send signed emails from ActionMailer? but it doesn't work on Rails 3.
Is it possible to sign emails via ActionMailer in Rails 3?
If that is not possible, is it possible to sign emails via sendmail after creating by ActionMailer?
perhaps it's not the best answer, however here's what I'd do:
try to install that plugin (even if it's for rails 2.0.x)
tests and fix code until I get the result
looking at the code, turns out that the core file is:
https://github.com/penso/actionmailer_x509/blob/master/lib/actionmailer_x509.rb
which exposes a bunch of methods for mailer DSL:
x509_sign true # or false
x509_cert "path/to/cert"
x509_key "path/to/key"
x509_passphrase "passphrase"
so you could grab that file, and put it under $APP/lib, then write some test to check it's working.
A.
I have been ported actionmailer_x509 to Rails 3 and wrap it into the gem. So now it works and available here: https://github.com/petRUShka/actionmailer_x509
I am using Ruby on Rails 3 and I would like to implement some Rack middleware.
I know (but maybe I am wrong!) that, before of version 3, there was the Rails Metal "system" to handle those. But now?!
Where in my Rails application I have to locate files to use for middleware purposes (before of version 3, if I am not wrong, the folder was named 'metal')? then, how I must state them in the application.rb file?
Check out the Rails on Rack page on Rails Guides.
Your own middlewares can be stored in lib and can be required at the top of application.rb.
Checkout the following blog post, that may answer your question:
http://tektastic.com/2010/07/rails3-rack-and-where-did-my-metal-go.html
Check out this http://www.engineyard.com/blog/2010/rails-and-merb-merge-plugin-api-part-3-of-6/ for how to create a "Metal" controller
http://www.ruby-on-rails-outsourcing.com/articles/2010/05/28/how-to-create-your-own-rack-middleware-class-in-ruby-on-rails/ may be helpful for you.
It basically suggests to put your class in /lib/class_name.rb and then instruct it to be used like
Rails::Initializer.run do |config|
config.middleware.use "ClassName"
...
I believe you are right there is no more "metal" option in rails 3 http://tektastic.com/2010/07/rails3-rack-and-where-did-my-metal-go.html you have to use a rack middleware instead. I'm unsure if this causes a performance hit or not.