Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Update:</strong> Way to access current_user and current_company rails methods in ember app described at bottom of answer and the <a href="https://github.com/blangslet/ember.js-rails-devise-demo" rel="nofollow noreferrer">github demo app</a> has also been updated.</p> <hr> <p>If I understand correctly what you're trying to do, below is one way to get the job done quickly. If anyone has a better answer, please edit my question. I have put up on <a href="https://github.com/blangslet/ember.js-rails-devise-demo" rel="nofollow noreferrer">github an ember + rails + devise demo app</a> that combines the answer to this question and <a href="https://stackoverflow.com/questions/14601092/ember-js-sorting-and-filtering-children-of-a-hasmany-relationship-in-parent-rout">my last ember.js stackoverflow question</a> to show that everything is working as it should be. Below I use the model Post instead of Article, but it's basically the same concept.</p> <p>1) In the Rails Companies view, on specific company link_to click, you should pass the company variable to a rails controller action that's passed onward to an application helper method to set a devise session variable for current_company. </p> <pre><code>&lt;% @companies.each do |company| %&gt; &lt;%= link_to 'Company Dashboard', set_current_company_company_path(:id =&gt; company) %&gt; &lt;% end %&gt; class CompaniesController &lt; ApplicationController def set_current_company session[:company_id] = params[:id] redirect_to assets_path end ... end class ApplicationController &lt; 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 ... </code></pre> <p>The set_current_company method will set the current_company variable session-wide and redirect the user to the assets/index.html.erb rails view that contains your ember application. </p> <p>2) You now need to scope your rails api data for Posts (and Comments, Company_Memberships) for current_company that you want to use in your json api/ember models, like so: </p> <pre><code>class PostsController &lt; 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 =&gt; 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 </code></pre> <p>3) Then you should serialize the data through AMS to the ember app in a normal way:</p> <pre><code>class PostSerializer &lt; ActiveModel::Serializer attributes :id, :title, :body, :company_id, :user_id has_many :comments end </code></pre> <p>4) Then on to the ember model. </p> <pre><code>App.Post = DS.Model.extend({ title: DS.attr('string'), body: DS.attr('string'), comments: DS.hasMany('App.Comment') }); </code></pre> <p>5) Ember Controllers, Views and templates should behave as expected. Check out the <a href="https://github.com/blangslet/ember.js-rails-devise-demo" rel="nofollow noreferrer">demo app</a> to see super basic implementations.</p> <hr> <p>In order to gain access to current_user and current_company, use <a href="https://github.com/rails-api/active_model_serializers#attributes" rel="nofollow noreferrer">active_model_serializers meta data serialization</a> and then <a href="https://stackoverflow.com/questions/14322945/accessing-meta-information-passed-in-a-json-server-response/14357404#14357404">this guy's temporary solution</a> to map your serializer so that it grabs the meta data and sets it to a global variable in your ember app. This may not be best practices, but for now, it gets the job done.</p> <p>1) First, set your store.js grab the meta data from your json and set it to a global variable in your serializer: </p> <pre><code>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(); } </code></pre> <p>2) Second, in your rails company_memberships_controller.rb render the meta data:</p> <pre><code>render json: @company_memberships, :meta =&gt; {:current_user =&gt; current_user, :current_company =&gt; current_company} </code></pre> <p>3) Finally, call any current_user or current_company attribute straight from your templates:</p> <pre><code>Logged in with: {{App.metaData.current_user.email}} &lt;br&gt; Current Company: {{App.metaData.current_company.name}} </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload