Resource Generators in Sinatra - ruby-on-rails

I have developed few apps in Rails, and I needed to develop an API. I received the advise to build it in Sinatra, so I started looking into it.
It seemed quite nice, but it seems that a lot of things you get automated in Rails does not seem to exist in Sinatra. Specifically, I seem to have to write my resources from scratch. eg. The model itself, the migrations, and the REST routes.
I was wondering if there are any generators for Sinatra like the ones provided by Rails? Or should I simply use Rails if I want these kind of things automated?

Check out the sinatra-rest gem that can be used to handle RESTful routes. Quoted below for convenience:
[sinatra-rest is] a set of templates to introduce RESTful routes into Sinatra. The only thing for you to do is to provide the views. Automatically works nicely for models based on ActiveRecord, DataMapper, or Stone.
For example, if your model’s class is called Person you only need to add this line:
rest Person
Which will add the following RESTful routes to your application. (Note the pluralization of Person to the /people/* routes.)
Verb Route Controller View
GET /people index /people/index.haml
GET /people/new new /people/new.haml
POST /people create → redirect to show
GET /people/1 show /people/show.haml
GET /people/1/edit edit /people/edit.haml
PUT /people/1 update → redirect to show
DELETE /people/1 destroy → redirect to index

I don't personally use Sinatra but a lot of feedback I've gotten from other Rails developers is that they eventually end up switching back to Rails. I'm sure there are good arguments for using Sinatra over Rails, but if you already know Rails, and you don't have speed or application size constraints, I would just stick with that.
Another alternative to Sinatra is the Rails API project which doesn't include any of the view-related part of the Rails framework. I have used that in the past and liked it, but was it better than just using Rails? It's hard to say.

You should check out Padrino if you must have Sinatra.
Padrino is a ruby framework built upon the Sinatra web library.
Sinatra is a DSL for creating simple web applications in Ruby. Padrino
was created to make it fun and easy to code more advanced web
applications while still adhering to the spirit that makes Sinatra
great!
Or as Beerlington mentioned, you could use Rails API if you feel more at home with Rails. We've been using it recently with good success. We created a Simple API, with a mongo backend. Starts up very quickly :)

Or should I simply use Rails if I want these kind of things automated?
If you're that used to Rails that using Ruby is a problem, then maybe. Alternatively, you could try this API generator that uses Sinatra:
https://github.com/mattetti/Weasel-Diesel

Related

What would be the best way to use AngularJS with Ruby on Rails?

I'm about to start a new project and I am unsure if using AngularJS for my front end would be a good idea not. I've read about people saying this isn't the smartest way of doing a project. And even if I did, Is there a way to get AngularJS to interact with the controllers? This question may be redundant but I am actually curious of how to effectively do this without it being a waste of time.
I've never completely done it, but I believe the way to go is to build a Rails api and then have a separate Angular project use said api. The api could also be used to build a mobile app. I think the Angular project would still need to be served from a Node.js server in production, but I don't think that would be a big deal.
This is what I used to learn how to build a Rails api: http://apionrails.icalialabs.com/book/chapter_one
You can do it within an existing project and share the models from it.
There are several different approaches to accomplish that. I tried about 5 different guides out there, the best I found (and I finally sticked to) was https://thinkster.io/angular-rails - This guide should help you build a basic CRUD app with angular connected to rails.
You use Rails as an JSON RESTful API which responds to Ajax-Requests (Get, Post, Put, Delete). Angular will handle the frontend stuff - sending those Ajax requests to the routes/methods defined in your rails controllers. So yes, of course your AngularJS app can interact with your rails controllers.
This also helped me to understand the setup in the beginning: Instead of the Rails View, you will be using AngularJS as your view:
I really love using angular with rails, because setting up the JSON responses (especially with Active Model Serializer Gem) is very easy and quickly done. i deffinitely can recommend it, and I have not encountered any unsolvable problems - so far.
Just go trough this guide I linked and you will see if this setup fits your needs.
The short answer is that your Rails application will have to present some kind of a public API for your AngularJS application to consume. Angular (and it's brethren, like React and Ember) runs client-side, on the browser, and it needs "something" to make AJAX calls against. That "something", i.e. your backend, can be Firebase, Parse, AWS Lambdas, Rails API, etc. Since you already have a Rails application, it probably makes the most sense to add some RESTful API endpoints that use the existing models (and possibly controllers) to consume/produce JSON payloads from/for the client.

How do I use rails helper in an emberjs application

I want to use rails helper method, for example I want to use form_for or root_url or devise's sign_in?, how could I do that?
Using Rails helpers inside your Ember application isn't really something that is generally feasible. You can get creative and do it by dynamically generating JS server-side, but it's not something I would consider best practice.
I could imagine it being useful to export some set of your Rails routes as JS that the application could use.
The short answer is, you can't, at least not "out of the box".
The longer answer is... any kind of tight coupling like this between Rails and Ember would ultimately be bad for both frameworks if it was included by default.. you'd be forced to use Ember with Rails, and vice-versa. As it stands, Ember is pretty agnostic about your server backend, as long as it returns a proper JSON response Ember doesn't care if it's Ruby (Rails or Sinatra typically), PHP, Node.js or even static .json files. That gives you a lot of flexibility when building your app, but it also makes it impossible to assume things like the Rails router (or the Rails form_for helper) should exist.
If you want something like a form_for helper, your best bet would be to either write it as a Handlebars helper or (better option in my opinion) a custom View class and a handful of Handlebars helpers to give you most of what the Rails route helpers give you.
For the route helpers, you'll want to find an automated way to export your actual Rails routes to Javascript, then go from there. For a good starting point, checkout this question
Accessing rails routes in javascript

What's the benefit of using Sinatra instead of RoR if I'm only need a DB and an API

I need to build a web service, for a mobile game, to manage the states of multiplayer games. I need a database and an RESTful API to access it. I'm very familiar with Ruby On Rails and was thinking of using that since I can throw together the DB and API pretty quickly. However, since RoR is a framework for building web pages and I'm not actually building any web pages, it naturally seems like the wrong technology to use even though it would work. As such, I'm considering using Ruby on Sinatra, but I've never used it before and I'll have to kill some time learning it. For you Ruby gurus, is there an advantage to using Sinatra or a disadvantage to using RoR for what I'm trying to accomplish?
Thanks so much in advance for your wisdom!
You know Rails, you don't know Sinatra. Personally I prefer the latter for things like building APIs, but there's nothing stopping you from doing it in Rails, and there's nothing intrinsically wrong with it either. Unless you want to see this as a learning opportunity for getting into Sinatra, I'd say stick with Rails. Here's some links that might be useful btw:
Building APIs With Rails
Building a Platform API on Rails
It probably depends on your API. If you need more than just a bunch of routes then you will have to come up with your own solutions (authentication, ...).
If all you need is some RESTfulness without the added weight, Sinatra is great. All you need to know is what happens in what route and you're fine. See the Sinatra Readme which has all the information to get started.

Looking for a fully functional Rails application using Backbone.js

Backbone.js website has some examples. But barring the first one others are not open source. I am looking for a fully functional (meaning it just works) Rails application to study. The app does not need to have too many functionalities. I looked at github and all the apps are broken in some ways.
Recently i found https://github.com/malclocke/fulcrum and it seems to be the best Rails/Backbone example but its not mentioned on the backbone website. Its also a very functional pivotal tracker clone.
I have been working on some non open source projects that use a Rails and Backbone.js stack. Both frameworks can be integrated fairly easily. Of course, it depends on how the application is setup and how you configure each framework to control more or less business logic.
To get both frameworks to play with each other:
Make Backbone collections and models for each Rails model
Route resources for each Rails model
Setup the URL property for the Backbone collections and models to work with your rails routes
Use fetch() and save() in Backbone to get and post data with Rails
I wrote a german language noun trainer using RAILS and backbone.js. It was done a long while ago while I was still learning but you can peek at it if you want.
https://github.com/bradphelan/ohmyderdiedas
I've been actively working on Myelin: http://sourceforge.net/projects/myelin/ (funded from a corporate source)
There are some caveats:
This is essentially a first for me with every technology in there... from rails, to backbone / jquery / rspec... you name it... it's new, so take the code with some grains of salt ;)
I didn't use the Backbone routing, and built a very simple 'router' of my own.
You'll need ganglia and rrdtool installed (macports if you're on a mac should work)
You'll need to alter the development config for sure.
The models, are (mostly) straight up backbone, and I use sync often in the controllers so those should be pretty good examples.
The views are a little more chaotic.
If anyone needs help with anything, just drop a line to me on sourceforge.
There's now a gem in development that provides generators, called rails-backbone. It's Open Source and getting better every day. As of today it's up to date with current Rails 3.1 (actually 3.2 now), esp. including Asset Pipeline, which is very relevant to backbone.js.

Best Way to Handle the UI Layer Within a Ruby Architecture

Are there some tools like CMS or UI-level frameworks that help in the maintenance and development of the UI layer within Ruby architectures?
I am still learning Ruby but as I understand it, it is very coupled with the front-end, correct?
Thanks,
Alex
You can use active_scaffold to get ajaxified crud interface and it can get integrated inside your existing rails app easily.
github.com/activescaffold/active_scaffold/wiki/Getting-Started
You can also use hobo but from I have gathered you need to use it right from the beginning as its generator creates the app.
hobocentral.net/
You can use comatose cms for integrating a basic content management functionality in your existing ror app.
comatose.rubyforge.org/getting-started-01.html
Also if you are looking for something pre-packed with rails then you can use command:
$ rails generate scaffold Post name:string title:string content:text
Command above is specific for rails 3 (in previous version scaffolding was available via ruby script/generate scaffold).
guides.rubyonrails.org/getting_started.html#getting-up-and-running-quickly-with-scaffolding
Hope this helps.
Are there some tools like CMS or UI-level frameworks that help in the maintenance and development of the UI layer within Ruby architectures?
CMS and UI level frameworks are very broad categories that you are talking about. Just to get you started, I will list down some I started with
Nifty-generators
blueprint-css
jquery-rails
I am still learning Ruby but as I understand it, it is very coupled with the front-end, correct?
No, its not very coupled with the front end as you think it is. You can assume Rails(Ruby) as just the back-end that will just send back data and when data has arrived at front-end, its your wish how to present it, format it or whatever you want to do with it.

Resources