更新:回答の下部に記載されている ember アプリの current_user および current_company レール メソッドにアクセスする方法と、github デモ アプリも更新されました。
あなたがやろうとしていることを私が正しく理解しているなら、以下は仕事を素早く終わらせる1つの方法です. 誰かがより良い答えを持っている場合は、私の質問を編集してください。この質問への回答と最後の ember.js スタックオーバーフローの質問を組み合わせて、すべてが正常に機能していることを示すember + rails + devise デモ アプリを github に投稿しました。以下、Article の代わりに Post というモデルを使用していますが、基本的には同じ概念です。
1) Rails Companies ビューで、特定の会社の link_to クリックで、company 変数を 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 変数をセッション全体に設定し、ユーザーを ember アプリケーションを含む assets/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) まず、json からメタデータを取得する store.js を設定し、それをシリアライザーのグローバル変数に設定します。
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) 次に、レール 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}}