- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
抱歉,我在 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 应用程序中访问 current_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/
这是我正在调试的函数: boolean adin_memory(char* buffer, int size_chunck, int end_flag){ global_buffer = my
我正在尝试为具有自由 float 底座的机器人计算末端执行器空间速度雅可比行列式。由于自由 float 基数,雅可比应该包含一个基数组件和一个操纵器注释(参见 https://spart.readth
procedure FreeListObjects( l : TStrings); var i : integer; BEGIN FOR i := 0 TO l.Count -1 DO BEG
我正在探索 Haskell 中的选项,这些选项可以让我将业务逻辑与底层系统的技术实现分开。例如,在 Web 服务器的上下文中,将 Web 服务器处理其接收的信息的方式与其读取和写入数据库的方式分开。要
我的目标是使用来自 ActiveMQ 的 WebSphere Liberty Appserver(完整的 Java EE 标准)使用消息。不幸的是,我不知道如何配置 WebSphere Liberty
我以这种方式分配了一个非方阵,但我不确定我是否正确使用了释放 float **matrix_alloc(int m /* rows */, int n /* columns */) { int
我在阅读 refuting the notion 之后的第 13.5 节内置运算符不参与重载决议,并注意到没有关于 operator->* 的部分。它只是一个通用的二元运算符。 它的兄弟operato
我正在尝试使用 Libelf 库来获取有关某些 elf 文件的一些信息。但我不断收到这些“对 [...] 的 undefined reference ”。我从 synaptic 安装了 libelf(
我有创建动态结构数组的波纹管代码。 #include #include #include typedef struct { int flag; char* ip; } ip_mo
我是 StackOverflow 的新人。我现在正在学习C指针。 这是我的代码: #include #include int alloc(int* p){ p = (int*) mallo
我是 StackOverflow 的新人。我现在正在学习C指针。 这是我的代码: #include #include int alloc(int* p){ p = (int*) mallo
我正在用 C 编写一个程序,我需要读入一个文件并打印出每个至少 4 个字符长的字符串。我在分配要使用的内存时遇到问题。字符串可以任意长。我试图将缓冲区分配给文件的大小,然后在最后释放它,但我显然错过了
我尝试用 C 语言编写 ls 命令,但 -R 选项有问题。 输出: /Applications/Atom.app/Contents/Resources/app/apm/node_modules/es5
我正在编写一个 shell,但在执行内存检查时遇到问题,因为 valgrind 无法正常运行。 我遇到了这样的错误(我自己杀死了它): ==19703== Memcheck, a memory err
我有这样一段代码: void *write_parallel(void *num_for_chunk) { struct rusage *sum = (struct rusage*) mall
当使用包含 200-300 个整数(以空格分隔)的输入 .txt 文件运行此代码时,我在使用 fprintf 语句的 for 循环之前收到错误。 我不确定 qsort 是否导致了此错误或为什么会发生此
我试图告诉 Websphere Liberty 我的 log4j2.xml 文件在哪里,但它不起作用。 在我的文件 jvm.options 中,我配置: -Dlog4j.configurationFi
从 websphere liberty 16 迁移到 19.0.0.1 我遇到以下异常:运行存储过程后关闭连接,出现以下异常: EJB threw an unexpected (non-declare
当对大小为 210*8 的种子数据集运行此代码时,我在预测函数中的 qsort() 行之后收到错误。它不在 qsort() 之后执行。 我不确定 qsort 是否导致了此错误或为什么会发生此错误,但如
这个问题已经有答案了: Facing an error "*** glibc detected *** free(): invalid next size (fast)" (2 个回答) 已关闭 9
我是一名优秀的程序员,十分优秀!