使用Rails帮手角模板 [英] Using Rails helper in Angular template

查看:107
本文介绍了使用Rails帮手角模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Rails与AngularJS,我想用导轨帮手和ERB-风格如果语句在角模板。

I am using Rails with AngularJS and I would like to use rails helper and erb-style if statement in an Angular template.

我显示删除按钮,并要确保只有所有者才能看到此按钮。

I am displaying a delete button and want to make sure only the owner can see this button.

在一个正常的 ERB 文件,我会写:

In a normal erb file, I would write:

<% if @article.id == current_user.id %>
  <button>Delete</button>
<% end %>

我试图 ERB 扩展添加到我的模板文件,但我不能使用嵌入的Ruby语法,也没有帮手 CURRENT_USER

I attempted to add erb extension to my template files, but I could not use embedded ruby syntax, nor the helper current_user.

我把另一种方法是将 NG-秀=所有者的按钮,并定义所有者功能在角控制器,以确定当前用户是否是所有者。但正如我没有进入这个没有工作 CURRENT_USER

Another approach I took was adding ng-show='owner' to the button, and define owner function in the Angular controller to determine if the current user is the owner. But this did not work as I don't have access to current_user.

有什么建议?如果有更多的角度办法做到这一点,我也想知道。

Any suggestions? If there is a more Angular way to do this, I would also like to know.

推荐答案

我觉得跟AngularJS的最难的事情之一是切换到不是直接在服务器上可用的数据,从服务器检索数据的心态。

I think one of the hardest things with AngularJS is switching to a mindset of "data retrieved from the server" not "data available directly in the server".

也就是说,你需要放弃,你可以用你的模板的轨道般的约定(同样适用于Django的或PHP或任何其他框架)的想法。

That is, you need to abandon the idea that you can use any rails-like conventions in your template (same goes for Django or PHP or any other framework).

相反,想想你怎么能一个API添加到您的Rails code,返回的数据问题在干净的JSON格式。然后编写调用它的角度服务,比如 ArticleService.get(ID),然后你的角模板将是直接的。

Instead, think of how you can add an API to your Rails code that returns the data in question in a clean JSON format. Then write an angular service that calls it, say ArticleService.get(id), and then your angular template will be straightforward.

这篇关于使用Rails帮手角模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆