gpt4 book ai didi

ruby-on-rails - Rails + Ember.js + 设计自由记者应用程序 - 数据范围

转载 作者:行者123 更新时间:2023-12-02 05:58:20 25 4
gpt4 key购买 nike

抱歉,我在 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。

我想设置一个流程,让用户访问网站、登录(使用设计非常简单),然后重定向到一个 Rails 页面,用户可以在其中选择他想访问的公司的仪表板(只需单击一条链接)。

现在是棘手的部分。当他点击特定公司的链接时,他应该被定向到一个 Rails View ,其中包含一个序列化范围设置为 current_company_membership 或 current_company 而不仅仅是 current_user 的 ember 应用程序。

我如何创建一个 rails 或 active_model_serializers 方法,将数据范围限定为 current_company_membership 指定的 current_company,然后将此数据传递给 ember 应用程序?


此外,作为奖励,我需要 current_user 的姓名和电子邮件地址 - 我如何将这些信息也传递给 ember 应用程序?我看过this dude's文章,但在尝试仅通过 company_membership 而不是 current_user 来确定数据范围时无法使其正常工作。

任何帮助都会很棒!如果需要,我会添加更多细节和代码,但我觉得我在这里忘记了一些非常简单的东西。

最佳答案

更新:在答案底部和 github demo app 中描述的在 ember 应用程序中访问 c​​urrent_user 和 current_company rails 方法的方法也已更新。


如果我正确理解您要执行的操作,下面是一种快速完成工作的方法。如果有人有更好的答案,请编辑我的问题。我已经忍受了 github an ember + rails + devise demo app结合了这个问题的答案和my last ember.js stackoverflow question以表明一切正常。下面我使用模型 Post 而不是 Article,但它基本上是相同的概念。

1) 在 Rails Companies View 中,在特定公司 link_to 单击时,您应该将公司变量传递给 Rails Controller 操作,该操作再传递给应用程序帮助程序方法,以便为 current_company 设置设计 session 变量。

<% @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 方法将在整个 session 范围内设置 current_company 变量,并将用户重定向到包含您的 ember 应用程序的 assets/index.html.erb rails View 。

2) 您现在需要为您想要在 json api/ember 模型中使用的 current_company 的帖子(和评论、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) 然后是 Ember 模型。

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

5) Ember Controller 、 View 和模板应按预期运行。查看demo app查看 super 基本实现。


为了获得对 current_user 和 current_company 的访问权限,请使用 active_model_serializers meta data serialization然后 this guy's temporary solution映射您的序列化程序,以便它获取元数据并将其设置为您的 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}}

关于ruby-on-rails - Rails + Ember.js + 设计自由记者应用程序 - 数据范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15024649/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com