So as you may know Rails can work as an API only, this will prevent view generations and some other stuff.
So now I installed the Devise gem but I don't know how to start working with it.
I'm using a ReactJS front end but how do I create a user for example? With the views I figured it out before but not with only json requests.
my comment being said, and vote for close being done, here's my answer to you:
if you don't use rails own views, it's fine, though that means you'll have to build manually all the basic CRUDs for each model element you'll want to expose. Or you can have a backoffice in vanilla rails that will help you manage the basic model elements, and a frontoffice communicating through the API (because you can have both).
Related
I'm goint to create a RESTfull service app is made of Rails5 with API mode.
I also need an admin app that provides web views for managing users and contents.
These two apps will share codes each other.
I know a way of creating the API mode app.
$ rails new apiapp --api
How do I create the other project?
The way I would implement this kind of functionality is like so:
RAIL API for your model, database, validation and relationships logic.
Client side MVC for Admin app with RESTful calls. For this I would use Backbone Marionette.
this is the cleanest, least code repetition implementation I can think of, which follows industry standards.
this is as per the software mantra 'consume your own dog food' - if you create an api, use its interface to do your stuff. this way you test and improve it as you go.
If you want RAILS only on both ends, you would be better off implementing your ADMIN and your API as one app, for least code repetition. Create an API controller name space for all your exterior calls, and code normal rails for your admin views and stuff. this way your database and model validation and relationship logic is shared, but controllers and route namespaces are not.
Toodles.
You build the other project as a normal rails project. The thing to understand about Rails 5 api mode is that you cannot have normal html stuff as part of it. The entire rendering pipeline (assets and such) is missing. Rails 5 api mode is fast because big parts of the environment are just plain gone.
What you want to do is have 2 projects:
admin
api
And figure out a way to share your model logic across them.
If you use devise for authentication this is particularly tricky since devise adds things into your user model that you cannot have in an api project. Here's how I got around it:
If Rails.application.class.parent_name == "admin"
# devise crap goes here
end
How exactly you easily share a directory of models across 2 git repos? I have no good answer. I have a rake task which sync's things manually by copying them from the canonical source to the destination but that's a hack.
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.
I'm developing a iOS app and want it to talk to a rails server.
I was wondering the normal approach for making API's for iOS consumption is? My original plan was to develop the site in rails with the functionality and the way I want it to work and then take that site and create an API for it. This seems overkill considering I don't want a web version of the app.
I'm interested to hear if anyone has had any experience with approaching this or how they would go about it.
I know I could develop the API standalone but am unsure how to develop the site functionality within a standalone API without views e.g using the rails-api gem.
Sorry if this questions is not explained well as I'm still relatively new to rails.
Thanks.
Assumption: rails 4+, you want the API to be JSON based.
If you use scaffold to create your model objects the you've got the API pretty much written for you. I mean it will create the views for you, and it's up to you to do any changes you want to the controller.rb (probably not) and to the view (action.json.jbuilder).
http://guides.rubyonrails.org/v3.2.13/getting_started.html
(go to) 5 Getting Up and Running Quickly with Scaffolding
A common change you'll make will be formatting a datetime property from your Rails model to be a certain format (lets go with unix timestamp), so you put those changes in your action jbuilder file, i.e
app/views/person/show.json.jbuilder
json.extract! #person, :first_name, :last_name, :id
json.date #person.date_of_birth.to_i
So now when you browse to
/person/23.json
you'll get
{
"first_name":"Rails",
"last_name":"is great",
"id": 23,
"date_of_birth": 1395101106
}
In summary, use
rails generate scaffold model_name property:string other_property:int
for your model objects
Use ActiveModelSerializers with the JSONAPI adapter, and jsonapi-ios on the iPhone. That way you have a well thought out JSON format out of the box.
For authentication I'd recommend Devise with devise_token_auth, and for roles the cancancan gem.
I need some guidance to figure out how to incorporate Angular inside rails.
Reason for choosing Rails : I like their opionated approach to do things right. Also migrations, gems are really cool.
Reason for angular : I was researching and looking for framework best suited for SPA. Backbone seem too abstract. I had choice to make between Angular & Ember. I started reading Angular first and it made sense to me. So i never went to read about ember.
Reason for Angular & Rails: I researched and tried using small scale framework like grape, slim ( Yes i use php too ). But i feel need to stick to rails for long term scope of project. And personally i like doing things rails way.
So here is where i need help,
I have rails project in Rails 4.
Sign-in , sign-up everything is created as followed in Michael Hartl tutorial. Have tweaked stuff based on my requirement.
So post-sign-in or post-sign-up steps a view from show action of users controller is rendered.
I figured i'll need different layout files so i created same for outer pages and inner pages, respectively.
I don't know how to proceed i want to make use of the angular templates and routes for my single page app ( which resides post sign-in ). I am flexible if there is another way. I just need a guide how to use angular seemlessly with rails making use of rails controller to handle my rest request and using routing provided by angular to navigate around in SPA.
Hope i am clear. Feel free to edit this.
here is a great railscasts from ryan bates: http://railscasts.com/episodes/405-angularjs
also here is the source for that railscasts you can get ideas from there:
https://github.com/railscasts/405-angularjs
I'm not familiar with SPA, but I have been working on a tutorial for integrating rails and angularjs, which I'm refining over time. It may provide some answers here - in particular I am using the angular routing to provide a single-page app as you describe: http://technpol.wordpress.com/2013/09/03/angularjs-and-rails-tutorial-index/
I would suggest you to not mix angular into your rails app. Keep both of them separate.
So you could either place the whole of your angular app in the public folder of your rails app or keep it completely away from the rails app. This is more like a service oriented architecture, where your rails app serves as a back end serving as an api and the front end(Angular app) consuming that api.
There are many many nice articles the covers how to handle the authentication/authorization
in angularjs with REST APIs.Authentication with AngularJS and a Node.js REST api
Coming to the rails side for building REST API, Grape is a nice choice. Here is a nice series explaining some best practices about grape.
HTH!
I'd suggest taking a look at this tutorial: How to Wire Up Ruby on Rails and AngularJS as a Single-Page Application. I have used it for a few personal projects, so I am sure that it is up to date as of late 2014. If you want to view it in action you can head to http://goodmatches.herokuapp.com, and you can view the repo.
Try half-pipe gem which makes using bower for managing javascripts assets much easier.
I mean, the mvc for cairngorm and the one in rails don't overlap their functionalities? I'm not sure I understand the need for cairngorm with the rails backend..
There is no need, it's an option, Cairgorm see's the whole Rails as a model. Rails send xml instead of html so no browser-css-details headaches.
You can use rails as a REST layer and put your business logic in flex.
You can use cairgorm as a candy cover over a complex but well tested rails application.
You can find middle of the way solution that suits to you.
We do implement most business logic in rails but use flex/cairgorm to show each user only the right buttons/forms/controls to her task, and to do it in a nice browser-independent way.
Hope this helps you, feel free to ask any detail
Rails is MVC for the server. You still need an architecture for a sufficiently complex client application. We're using PureMVC on the client with a REST (JSON) interface to a Rails server and it's working nicely. PureMVC allows for the client's model to be independent from the client's view components, and makes it easy to update independent view components simultaneously.