簡體   English   中英

Rails + Ember.js + Devise自由記者應用程序-數據范圍

[英]Rails + Ember.js + Devise freelance journalist app - data scoping

抱歉,我已經在ember.js領域呆了很長時間了,我在弄亂Rails + active_model_serializers JSON api基礎知識。

假設我是一名自由撰稿人,並且正在構建一個應用程序,作家可以通過一個用戶帳戶將文章發布到公司特定的不同站點。

Rails Relationships

User has_many :company_memberships
User has_many :companies, :through => :company_memberships

Company has_many :company_memberships

Company_membership belongs_to :user
Company_membership belongs_to :company

Article belongs_to :company_membership
Article has_many :comments

Comment belongs_to :article

我正在使用Rails 3 +,Devise,Active_Model_Serializers,Ember-Data和Ember.js。

我想建立一個流程,使用戶可以訪問該網站,登錄(非常容易使用devise),然后重定向到Rails頁面,在該頁面中,他可以選擇他想去的公司儀表板(只需單擊鏈接)。 。

現在是棘手的部分。 當他單擊特定公司的鏈接時,應將其定向到包含Ember應用程序的Rails視圖,該應用程序的序列化范圍設置為current_company_membership或current_company,而不僅僅是current_user。

如何創建rails或active_model_serializers方法,以將數據范圍限定為current_company_membership指示的current_company,然后將這些數據傳遞到ember應用程序?


另外,作為獎勵,我需要current_user的名稱和電子郵件地址-如何將這些信息也傳遞給ember應用程序? 我看過這家伙的文章,但是當嘗試僅通過company_membership而不是current_user來限定數據范圍時,卻無法使它正常工作。

任何幫助都是極好的! 如果需要的話,我將添加更多細節和代碼,但是我覺得我忘記了一些非常簡單的內容。

更新:答案底部描述了在ember應用程序中訪問current_user和current_company rails方法的方法,並且github演示應用程序也已更新。


如果我正確理解您要做什么,以下是一種快速完成工作的方法。 如果有人有更好的答案,請編輯我的問題。 我在github上放置了一個ember + rails + devise 演示應用程序 ,該應用程序結合了該問題的答案和我的上一個ember.js stackoverflow問題,以表明一切正常。 在下面,我使用模型Post而不是Article,但是基本上是相同的概念。

1)在Rails公司視圖中,單擊特定的公司link_to,將公司變量傳遞給Rails控制器操作,該操作將繼續傳遞給應用程序幫助程序方法,以為current_company設置設計會話變量。

<% @companies.each do |company| %>
    <%= link_to 'Company Dashboard', set_current_company_company_path(:id => company) %>
<% end %>

class CompaniesController < ApplicationController

  def set_current_company
    session[:company_id] = params[:id]
    redirect_to assets_path 
  end
  ...
end

class ApplicationController < ActionController::Base
...
  helper_method :current_company

  def current_company
      @current_company ||= Company.find_by_id!(session[:company_id])
  end
...
end

App::Application.routes.draw do

resources :companies do
    member do
      get :set_current_company
    end
  end
...

set_current_company方法將在整個會話范圍內設置current_company變量,並將用戶重定向到包含您的余燼應用程序的asset / index.html.erb rails視圖。

2)現在,您需要確定要在json api / ember模型中使用的current_company的Posts(和Comments,Company_Memberships)的rails api數據范圍,如下所示:

class PostsController < ApplicationController

 def index 
    @posts = Post.where(company_id: current_company.id)
    render json: @posts 
  end

def create
    @post = Post.new(params[:post].merge :company_id => current_company.id)

    respond_to do |format|
      if @post.save
         render json: @post, status: :created, location: @post
      else
    render json: @post.errors, status: :unprocessable_entity 
      end
    end
  end

3)然后,您應該以常規方式通過AMS將數據序列化到ember應用程序:

class PostSerializer < ActiveModel::Serializer
  attributes :id, :title, :body, :company_id, :user_id
  has_many :comments
end

4)然后到灰燼模型。

App.Post = DS.Model.extend({
  title: DS.attr('string'),
  body: DS.attr('string'),
  comments: DS.hasMany('App.Comment')
});

5)Ember控制器,視圖和模板的行為應符合預期。 查看演示應用程序以查看超級基本實現。


為了獲得對current_user和current_company的訪問權,請使用active_model_serializers元數據序列化 ,然后使用此人的臨時解決方案來映射您的序列化程序,以便它獲取元數據並將其設置為ember應用程序中的全局變量。 這可能不是最佳做法,但是到目前為止,它已經完成了工作。

1)首先,設置store.js從json中獲取元數據,並將其設置為序列化器中的全局變量:

App.CustomRESTSerializer = DS.RESTSerializer.extend({
  extractMeta: function(loader, type, json) {
    var meta;
    meta = json[this.configOption(type, 'meta')];
    if (!meta) { return; }
    Ember.set('App.metaData', meta);
    this._super(loader, type, json);
  }
});

App.Store = DS.Store.extend({
  revision: 11,
  adapter:  DS.RESTAdapter.create({
    bulkCommit: false,
    serializer: App.CustomRESTSerializer
    }),
});

App.ready = function() {
  App.CompanyMembership.find();
}

2)其次,在您的Rails中,company_memberships_controller.rb呈現元數據:

render json: @company_memberships, :meta => {:current_user => current_user, :current_company => current_company}

3)最后,直接從模板中調用任何current_user或current_company屬性:

Logged in with: {{App.metaData.current_user.email}}
<br>
Current Company: {{App.metaData.current_company.name}}

只需重寫序列化器中的方法即可。

def custom_association
  # scope is the current user
  CustomObjects.ownedBy(scope)
end

我不確定我是否完全了解您的問題,但是:

您可以在控制器類定義中(或在ApplicationController基類上)設置序列化范圍:

serialization_scope :current_company

您還可以在實例化范圍時將范圍傳遞給串行器。 因此,要返回record的JSON,可以在控制器方法中執行以下操作:

record.active_model_serializer.new(record, scope: current_company).as_json

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM