how to make a sinatra app run in rails 4? - ruby-on-rails

I have a Sinatra app and I'd like to start building new functionality in Rails while still supporting the existing Sinatra functionality. I've tried the following strategies:
sinatra's rackup routes some requests to rails and some to sinatra
sinatra's rackup includes rails
rails' rackup includes sinatra.
Many of my searches resulted in rails 3, not 4. Additionally, does Rails have to generate the db versus using one that Sinatra was using (in this case, the Sequel gem to access Sqlite3.) In general the errors I was getting were about gems and paths. (Although I did rebundle and try different versions of paths.)
Any suggestions on the best way to use Rails 4 while still supporting an existing Sinatra app?

I don't think the Rails/Rack integration code has changed very much between Rails 3 and 4, so you should be fine. The Rails on Rack Guide explains in more detail that you can make a config.ru file for a Rails application that looks like:
require ::File.expand_path('../config/environment', __FILE__)
use Rack::Debugger
use Rack::ContentLength
run Rails.application
and then running rackup config.ru will start a rack server running your rails app.
The answers to this question point out that if you run Rails and Sinatra from Rack, rather than mounting your Sinatra app in Rails' routes.rb file, requests to your Sinatra app won't go through Rails at all. The answers also show that in your config.ru you should be able to do this to support both your Sinatra and Rails apps:
map "/" do
run RailsApp::Application
end
map "/url1" do
run SinatraApp1
end
You'll have to modify the routes and application names to match your needs and your applications, of course.
I'd recommend getting your apps running through one config.ru first, and then ask another question about your databases, explaining in more detail what you'd like the database setup to be and what the exact error messages you're getting are.

Rails does not need to create a database or even use one directly. To generate a new Rails app without ActiveRecord, run rails new APP_PATH --skip-active-record. Then, instead of using a database directly from the Rails app, send requests to the Sinatra app and have the Sinatra app control everything database-related.

Related

Ruby on Rails run at Blogspot?

I tried to build a ruby-based website. but I want to start on blogspot which is free for beginners. can the ruby system be placed on blogspot, I am very happy if this happens.
No Blogspot does not support ruby on rails. Even it is just a blogging platform where you already have CMS to serve you. But you should have a development environment. Please try Heroku.com if you want free.
check this tutorial https://devcenter.heroku.com/articles/getting-started-with-rails5 to launch your first application on Heroku.
You can add the following lines after
rails generate controller welcome
rails generate scaffold product name 'price:decimal{7,2}'
rails db:migrate
This will create a sample application where you can do all CRUD operation on product. you can try following
rails s
Now visit localhost:3000/products
After installation on Heroku you will get your url where you can run app and do all CRUD operation. Just try it is very easy to do.
If you are working with Ruby on Rails, consider to work with another platform like Wordpress instead. But I think it takes time.

Automatic Reloading of Sinatra App mounted inside Rails App

I've run into what I hope is an easy-ish fix for someone more experienced than me when adding a Sinatra app to an existing Rails app.
I currently have a large rails monolith that I am planning to break apart into an SPA backed by a JSON API. Since I will still need to support the monolith until the API is completed I would like to mount the API (written in Sinatra) inside the existing Rails app as I port functionality over with a goal of removing the Rails app itself in a few months. The reason I've mounted the Sinatra app inside Rails instead of setting it up as a separate service was that I wanted easy code sharing between the two as I intend to continue using ActiveRecord as my ORM in Sinatra once the migration is complete.
I've mounted the Sinatra mockup inside the Rails app without any issues using the Rails routes.rb file as:
Rails.application.routes.draw do
mount API::Core, at: '/api'
...
end
I'm not doing any work with config.ru as mounting the API inside the routes.rb file seemed to suit my needs. If I need to put in some more legwork to get this running properly that is not an issue.
The entrypoint for the sinatra app is also fairly simple with just a couple controllers loaded in to segment the routing:
require 'sinatra/base'
require 'sinatra/json'
require_relative 'controllers/first_controller'
require_relative 'controllers/second_controller'
module API
class Core < Sinatra::Base
register Sinatra::FirstControllerApi
register Sinatra::SecondControllerApi
end
end
The problem I'm running into is one I expected with Sinatra but haven't been able to flex my google-fu enough to find a solution. Rails automatically reloads code on each change/request as expected but Sinatra does not. Every time I change controller code in the Sinatra API I need to restart the entire Rails server to serve the new content. Since my dev environment runs in docker containers this can take a while to start up each time and is becoming cumbersome.
Is there a 'canonical' solution to the problem of automagically reloading the Sinatra app mounted inside the Rails app or am I just over-complicating the problem? I know there are some gems for Sinatra that apply to this space but I haven't seen any info in how to get them working in this 'odd' edge case.
Please let me know if you need any more info, hopefully I've provided enough for someone to comprehend my issues.
Just like with a Sinatra app on it's own, a Sinatra app mounted inside o rails will not automatically reload as you make changes. Typically in Sinatra people would use external tools to help solve this problem, so they might set up something like Shotgun to watch the directory and restart the server any time a change has happened.
But, like most things in Rails, there is a way hook into it's functionatly for your own programming benefit.
ActiveSupport::FileUpdateChecker
Rails has a handy ActiveSupport::FileUpdateChecker that can watch a file, or a list of files. When they change it will call back to a block. You can use this to reload the Sinatra app -- or anything else for that matter.
In your case you might want to add the following to config/environments/development.rb in the config block for the application:
Rails.application.configure do
# ...
sinatra_reloader = ActiveSupport::FileUpdateChecker.new(Dir["path/to/sinatra/app/**"]) do
Rails.application.reload_routes!
end
config.to_prepare do
sinatra_reloader.execute_if_updated
end
end
Robert Mosolgo has a good write up on his blog: Watching Files During Rails Development. His approach is a little more thorough, but more complicated.

Can you convert a Rails app to Sinatra on Heroku

Does anyone know if there is any issues with converting a Rails app to Sinatra with Heroku? I already have a Heroku app running and it is currently a simple Rails app that is basically a few views and a controller with no database interaction. Are there any potential issues if I just change everything from Rails to Sinatra?
There shouldn't be any issues, other than the manual work required by you. You'll want to make sure and update your Procfile to run your Sinatra app correctly. See here for Deploying Rack-based Apps on Heroku's Devcenter. I also recommend using Foreman for local development, it should helping to find any possible issues running on Heroku easier: https://devcenter.heroku.com/articles/procfile#developing-locally-with-foreman

What should a Rails 3.x noob know about maintaining a Rails 2.3 app?

I recently learned Rails (Rails 3 to be specific) and got a small project maintaining a Rails 2.3.2 app.
So, as you might expect, I keep running into differences between what I learned in Rails 3 and what I'm working in.
For example 'rails server' command in Rails 3 is 'script/server' in Rails 2.x.
What else should I know to maintain my sanity?
If you're new to rails, I don't know if you'll see a lot of differences. The rails server command is one thing however.
The other big thing is the use of .gemspec in Rails 2.3.5 instead of Bundler and Gemfiles in Rails 3. Installing gems/specifying which gems your app uses is way better in Rails 3 I think.
The rest are mostly syntax changes and depreciated functions in specific areas (e.g. javascript generators).
This Blog does a good job of listing most of the major changes in more detail.
Hope that helps!

How to embed/mount existing Rails app into another Rails app?

I have two Rails apps (using rails 3.0.7), call them "blog" and "auth". I'd like to mount "auth" from "blog" such that I can run the "blog" app and have certain requests routed to the "auth" app.
It seems like I need to embed or perhaps create an "engine", but I'm not 100% sure which is correct.
How can I embed the "auth" app inside of the "blog" app?
You can create "rails-engine" for 'auth' app, and then mount this engine into the rails application i.e 'blog' application.
Read more about Rails::Engine at below links -
http://guides.rubyonrails.org/engines.html
http://api.rubyonrails.org/classes/Rails/Engine.html
To embed a Rails mountable engine into a Rails application, follow these general steps -
1) Open the target Rails project, where an engine should be embedded.
2) Open for editing the Gemfile, and add the following line:
gem '<engine name>', :path => "<absolute path to the Rails mountable engine project>"
3) Open for editing Config/routes.rb, and add the following line:
mount <engine name>::Engine, :at => "/<engine name>"
Rails was raising RuntimeError: You cannot have more than one Rails::Application if you attempted to have two Rails apps defined in one Ruby instance, but it has changed after this commit.
This is still unreleased in 4.0.0, but will get included in the newer versions. (> 4.1.0.beta)
Check out the discussion on PR for more info.
From what I understand, you probably don't need to have two Rails apps, though. You should probably attempt to extract functionality you need in the Rails::Engine.
Remember, Rails::Application is also a Rails::Engine.
You can find a lot of material about how to do it on the web, and I would recommend these two to get you started.
Rails3 is a rack-based application. Rackmiddleware already has a number default built-in applications. Try to run bundle exec rake middleware. You can add custom Rack applications into the stack. Use this blogpost to see how it works.
If auth app is something like a web service (for example facebook authentication), so just keep 2 apps running and make requests from one app with urls to another app.
If these apps share some logic or the same database, you can just merge the code, so two apps become one, or your can make 'auth' app something like a gem or a plugin and use it in your 'blog' app.

Resources