gem 'oxymoron'
/* = require oxymoron/underscore = require oxymoron/angular = require oxymoron/angular-resource = require oxymoron/angular-cookies = require oxymoron/angular-ui-router = require oxymoron/ng-notify = require oxymoron = require_self = require_tree ./controllers */
/* *= require oxymoron/ng-notify *= require_self */
html ng-app="app" head title base href="/" = stylesheet_link_tag 'application' body ui-view = javascript_include_tag 'application'
layout proc { if request.xhr? false else "application" end }
ActionView::Base.default_form_builder = OxymoronFormBuilder
var app = angular.module("app", ['ui.router', 'oxymoron']); app.config(['$stateProvider', function ($stateProvider) { $stateProvider.rails() }])
rails g model post title:string description:text rake db:migrate rails g controller posts index show
Rails.application.routes.draw do root to: "posts#index" resources :posts end
class PostsController < ActiveRecord::Base before_action :set_post, only: [:show, :edit, :update, :destroy] def index respond_to do |format| format.html format.json { @posts = Post.all render json: @posts } end end def show respond_to do |format| format.html format.json { render json: @post } end end def new respond_to do |format| format.html format.json { render json: Post.new } end end def edit respond_to do |format| format.html format.json { render json: @post } end end def create @post = Post.new post_params if @post.save render json: {post: @post, msg: "Post successfully created", redirect_to: "posts_path"} else render json: {errors: @post.errors, msg: @post.errors.full_messages.join(', ')}, status: 422 end end def update if @post.update(post_params) render json: {post: @post, msg: "Post successfully updated", redirect_to: "posts_path"} else render json: {errors: @post.errors, msg: @post.errors.full_messages.join(', ')}, status: 422 end end def destroy @post.destroy render json: {msg: "Post successfully deleted"} end private def set_post @post = Post.find(params[:id]) end def post_params params.require(:post).permit(:title, :description) end end
PostsController => PostsCtrl Admin::PostsController => AdminPostsCtrl # namespace Admin
app.controller('PostsCtrl', ['Post', 'action', function (Post, action) { var ctrl = this; // '/posts' action('index', function(){ ctrl.posts = Post.query(); }); // '/posts/:id' action('show', function (params){ ctrl.post = Post.get({id: params.id}); }); // '/posts/new' action('new', function(){ ctrl.post = Post.new(); // , . . . ctrl.save = Post.create; }); // '/posts/:id/edit' action('edit', function (params){ ctrl.post = Post.edit({id: params.id}); // ctrl.save = Post.update; }) // . edit new. action(['edit', 'new'], function(){ // }) action(['index', 'edit', 'show'], function () { ctrl.destroy = function (post) { Post.destroy({id: post.id}, function () { ctrl.posts = _.select(ctrl.posts, function (_post) { return _post.id != post.id }) }) } }) // routes.rb . : '/posts/some_method' action('some_method', function(){ // }) // etc }])
action(['edit', 'new'], function(){ // posts/new posts/:id/edit })
Post.query() // => GET /posts.json Post.get({id: id}) // => GET /posts/:id.json Post.new() // => GET /posts/new.json Post.edit({id: id}) // => GET /posts/:id/edit.json Post.create({post: post}) // => POST /posts.json Post.update({id: id, post: post}) // => PUT /posts/:id.json Post.destroy({id: id}) // => DELETE /posts/:id.json
resources :posts do member do get "comments", is_array: true end end
Post.comments({id: id}) //=> posts#comments
h1 Posts input.form-control type="text" ng-model="search" placeholder="" br table.table.table-bordered thead tr th Date th Title th tbody tr ng-repeat="post in ctrl.posts | filter:search" td ng-bind="post.created_at | date:'dd.MM.yyyy'" td a ui-sref="post_path(post)" ng-bind="post.title" td.w1 a.btn.btn-danger ng-click="ctrl.destroy(post)" a.btn.btn-primary ui-sref="edit_post_path(post)"
.small ng-bind="ctrl.post.created_at | date:'dd.MM.yyyy'" a.btn.btn-primary ui-sref="edit_post_path(ctrl.post)" a.btn.btn-danger ng-click="ctrl.destroy(ctrl.post)" h1 ng-bind="ctrl.post.title" p ng-bind="ctrl.post.description"
h1 New post = render 'form'
h1 Edit post = render 'form'
= form_for Post.new do |f| div = f.label :title = f.text_field :title div = f.label :description = f.text_area :description = f.submit "Save"
<form ng-submit="formQuery = ctrl.save({form_name: 'post', id: ctrl.post.id, post: ctrl.post}); $event.preventDefault();"></form>
ng-model="ctrl._._"
a ui-sref="posts_path" a ui-sref="post_path({id: 2})" №2 a ui-sref="edit_post_path({id: 2})" №2 a ui-sref="new_post_path"
Routes.posts_path() // => "/posts" Routes.new_post_path() // => "/post/new" Routes.edit_posts_path({id: 1}) // => "/post/1/edit" // Routes.defaultParams = {id: 1} Routes.post_path({format: 'json'}) // => "/posts/1.json"
Source: https://habr.com/ru/post/283214/
All Articles