I'm a new in backbone.marionette. I read some guides, but not enough understood how is the MVC structure implemented there.
In rails I'm having structure like this:
app/
assets/
controllers/
models/
views/
config/
environmrnts/
development.rb
staging.rb
production.rb
routs.rb
log/
my view/js files are sending data to controller(during update/create and more), and and also getting data from it. the controller sends requests to model in another rails app, using Api class(that all the models are inherit from it) with net/https.
My questions are:
1. How do I implement the connection with the other app in backbone.marionette?
2. Is the idea of the mvc is the same as in rails? what actually the controller does in marionette?
3. How should my app structure look like?
4. Where should I store config parameters?
It goes through the API you define in the Rails app. Each Backbone model will define a url property to indicate where its data is stored. Then Backbone will take care of the rest (e.g. sending a POST request to the API to create a new model instance in the DB)
The idea is similar, but not the same. In Backbone, the controller and model behave mostly as in Rails apps. Collections are a group of models that you work with to make it easier in your app (e.g. displaying a list of users). Templates are sort of like views in Rails : they define the HTML markup that will get generated. Views in Backbone are very different : they react to the environment (e.g. user clicks, data modification) and drive the app's behavior. This doesn't happen in Rails apps because the page gets rendered and sent back : there is no itnteraction (eacch user click will make the server generate a new page and send it to the user).
It depends. There are many valid approaches, and you can see one here : https://github.com/davidsulc/marionette-gentle-introduction
It depends :-) Quite often, you will stroe them in a simple javascript object.
If you want something to guide you on your journey learning to develop javascript apps, take a look at these :
backbonerails.com uses Rails and Marionette to develop an application
my book on Marionette focuses more on explaining the various bits and pieces of Marionette, as well as how and when to use them
You can see an example of connecting to a different service using an API here : http://www.backbonerails.com/screencasts/loading-views starting at the 6:00 mark. The url property is defined at 9:40, but note that this case requires the url to be different for each collection instance, which might not be true in your case. If all collection instances have the same url, you'd simply define it as a property on the collection "class".
Related
I am new to development. I've read a few books on rails and often times they dive straight into examples. In some examples they generate resources which includes models, views, controllers etc. while in others they generate models only and vice versa. When should controllers be generated?It'd be great if someone could shed some lights to this to help me begin. Thanks.
You should do this tutorial : http://guides.rubyonrails.org/getting_started.html it pretty shows much everything about Rails concisely.
But basically ..
Controllers are files that are the first endpoint to routes. When you type an address in the navbar you end up in a controller first. Then, inside your controller an action is triggered (Show, Index, Update ...).
Actions can be blank it is not a problem. But most of the time there is some logic added. This logic can be loading some data, creating records in the database etc ...
Once the action is completed a view is triggered. This view corresponds to the controller action that has been just been visited. It is usually an ERB file that will eventually produce an HTML file after the server side scripting is completed.
Models sit alongside controllers and views: they handle all the database data. This includes validations for the fields of specific tables (does an uploaded file exceed max size, can a field of a record be blank etc ...). One database table = one model.
The most simple explanation would be :
Controllers talk to your views and models, they take requests from users (when a user is visiting your website, all requests go to the route.rb file, depending on how you set up the route.rb file the requests go to the right controller or directly to views, if you have static pages for example) and create responses.
Models are handling the hard stuff, they communicate with the database and add/remove/edit any new data in the database and provide the controller with the data it needs.
Views are just html files in which you can embed ruby code (views end with the .html.erb extension (erb is for embedded ruby)). They get the data they need from the database through controllers. The controller then sends the right views as a response to the user request.
When are you supposed to create a new view,controller or model? That's very hard to answer without an example. Every application is specific and require a good amount of experience to set up your MVC correctly.
This is the explanation from just another newbee in Rails, so don't take anything I wrote as completely true and correct.
I would suggest you to look into some books for beginners which walk you through the whole process of creating a fully functional website in Rails.
My suggestion would be to check out Michael Hartl's book The Ruby on Rails tutorial or if you want to take a step further and learn Ruby and RoR in detail, visit The Odin Project. Both are completely free and helped me a lot understand how Rails works.
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.
Hypothetical question (at the moment!)
Suppose I have a great idea for an application. It acts on data which can be well-represented by tables in a relational database, using interlinked objects which represent those tables. It supports a well-defined API for interacting with (Creating, Reading, Updating, Deleting) those objects, and viewing information about them.
In short, it's a perfect fit for Rails... except it doesn't want to be a web-app. Perhaps it wants a Command Line interface; or an OS-native dialog-based interface; or perhaps it wants to present itself as a resource to other apps. Whatever - it just isn't designed to present itself over HTTP.
These questions suggest it's certainly possible, but both approach the problem from the point of view of adapting an existing web-app to have an additional, non-web, interface.
I'm interested in knowing what the best way to create such an app would be. Would you be best to rails new non_web_app, in order to get the skeleton built "for free", then write some "normal" Ruby code that requires config/environment - but then you have a lot of web-centric cruft that you don't need? Or would it be better to roll up your sleeves and build it from whole cloth, taking just the libraries you need and manually writing any required configuration?
If the latter, what exactly is needed to make a Rails app, but without the web bits?
If you want to access the Rails ORM to develop a CRUD non-web application, just include ActiveRecord in your own Ruby script; you will avoid using a lot of Rails modules you probably don't need (routing, template generator, ...) Here is an example of how to do it.
If you prefer to have the full Rails stack, do not run your Rails web app in an application server (WEBrick, Passenger, Mongrel, ...) to avoid any HTTP exposure, and interact with your application using tasks or the rails console.
I would avoid taking Rails too far off the rails. If I were doing this and felt that the gains of rails w/o the web stuff I'd do the following:
rails new non_web_app
and ignore the webbish cruft and use rails to generate models. In this way you get the tight, comfortable database behavior and can tie various gems in as you want to augment those models. I'd not bother implementing views, of course, and I'd consider implementing controllers in which the various render bits are removed and to use you instantiate an instance of the controller and call the action directly. This means the controller represents your API into your business logic still but the "views" it now "renders" are simply the return of the data output.
Then you could simply strip out the bits you do not need...the public directory, the view structure under app, config/routes.rb, etc. You'll need to test those changes incrementally and make sure that removing some now extraneous bit doesn't throw the Rails world into chaos.
Rails is for Web apps. That means HTTP. Now, you could package a Web app so that it runs on the desktop instead, or you could use ActiveRecord with a desktop application framework like Monkeybars.
Background:
We have app a, b, and plan to add more apps into this same application. The apps are similar enough they could share many views, assets, and actions. Currently a,b live in a single rails app(2.3.10). c will be similar enough that it could also be in this rails app.
The problem:
As we continue to add more apps to this one app, there's going to be too much case logic that the app will soon become a nightmare to maintain. There will also be potential namespace issues. However, the apps are very similar in function and layout, it also makes sense to keep them in one app so that it's one app to maintain(since roughly 50% of site look/functionality will be shared).
What we are trying to do is keep this as clean as possible so it's easy for multiple teams to work on and easy to maintain.
Some things we've thought about/are trying:
Engines. Make each app an engine. This would let us base routes on the domain. It also allows us to pull out controllers, models and views for the specific app. This solution does not seem ideal as we won't be reusing the apps any time soon. And explicitly stating the host in the routes doesn't seem right.
Skinning/themes. The auth logic would be different between the apps. Each user model would be different. So it's not just a skinning problem.
In app/view add folder sitea for sitea views, siteb for siteb views and so on. Do the same for controllers and models. This is still pretty messy and since it didn't follow naming conventions, it did not work with rails so nicely and made much of the code messier.
Making another rails app. We just didn't want to maintain the same controller or view in 2 apps if they are identical.
What we want to do is make the app intelligently use a controller based on the host. So there would be a sessions controller for each app, and perhaps some parent session controller for shared logic(not needed now). In each of these session controllers, it handles authentication for that specific app. So if the domain is a.mysite.com, it would use session controller for app a and know to use app a's views,models,controllers. And if the domain is b.mysite, it would use the session controller for b. And there would be a user model for a and user model for b, which also would be determined by the domain.
Does anyone have any suggestions or experience with this situation? And ideally using rails 2.3.x as updating to rails 3 isn't an option right now.
Devise does exactly this. You would do well to check out its architecture and apply that architecture to your own case.
You will have multiple separate Rails applications. The shared code will be a separate project, perhaps distributed as a gem or at least a separate Git repository. The shared code will include many controller actions and many view templates that are there to be sensible defaults, and which will be overridden in some apps but not in others.
All the custom code for application A will belong in a project solely devoted to containing the custom code for application A. It will be its own fully-functioning Rails application and will depend heavily on the majority of the sensible defaults provided by the shared code in the shared-code project.
I've used the theme support plugin before and dynamically set the theme based on the request uri:
http://mattmccray.com/svn/rails/plugins/theme_support
It will probably need some work to support Rails 2.3.
Update: Looks like there's a rewrite: https://github.com/dasil003/rails-multisite
Sounds like you want to make the 'base' app a plugin and use that in each of your site apps. You can use something like svn-extern so it's automatically updated whenever something changes.
I have a Ruby on Rails (2.3.5) application and an APE (Ajax Push Engine) server. When records are created within the Rails application, i need to push the new record out on applicable channels to the APE server. Records can be created in the rails app by the traditional path through the controller's create action, or it can be created by several event machines that are constantly monitoring various inputstream and creating records when they see data that meets a certain criteria.
It seems to me that the best/right place to put the code that pushes the data out to the APE server (which in turn pushes it out to the clients) is in the Model's after_create hook (since not all record creations will flow through the controller's create action).
The final caveat is I want to push a piece of formatted HTML out to the APE server (rather than a JSON representation of the data). The reason I want to do this is 1) I already have logic to produce the desired layout in existing partials 2) I don't want to create a javascript implementation of the partials (javascript that takes a JSON object and creates all the HTML around it for presentation). This would quickly become a maintenance nightmare.
The problem with this is it would require "rendering" partials from within the Model (which im having trouble doing anyhow because they don't seem to have access to Helpers when they're rendered in this manner).
Anyhow - Just wondering what the right way to go about organizing all of this is.
Thanks
After talking w some folks in #rails and #ape this is appears to be the best approach to this issue.