2014-02-21 21 views
2

假设有一个Project的表单页面,它有namedescription。看看render :partial使用AngularJs的部分视图(在Rails中)的最佳方式是什么?

<div class="page-header"> 
    <h2>{{project.id ? 'Edit ' : 'Add new'}} project</h2> 
</div> 

<div class="row"> 
    <div class="col-xs-6 col-xs-offset-2"> 
    <form class="form-horizontal"> 
     <%= render :partial => 'templates/forms/text', :object => 'name', :locals => {:model => 'project'} %> 
     <%= render :partial => 'templates/forms/text', :object => 'description', :locals => {:model => 'project'} %> 
     <div class="form-group"> 
     <div class="col-xs-offset-3 col-xs-9"> 
      <button ng-click="saveProject()" type="submit" class="btn btn-success">Submit</button> 
      <a ng-href="{{ path('UserIndexCtrl') }}" class="btn btn-default">Back</a> 
     </div> 
     </div> 
    </form> 
    </div> 
</div> 

这里是部分被使用。

<div class="form-group" ng-class="{'has-error': errors.<%= text.underscore %>}"> 
    <label for="<%= model %>.<%= text %>" class="col-xs-3 control-label"><%= text.titleize.humanize %></label> 
    <div class="col-xs-9"> 
    <input id="<%= model %>.<%= text %>" name="<%= model %>.<%= text %>" type="<%= local_assigns[:type] || 'text' %>" class="form-control" ng-class="{'text-danger': true}" ng-model="<%= model %>.<%= text %>"> 
    <span class="help-block" ng-show="errors.<%= text.underscore %>"><%= text.titleize.humanize %> {{errors.<%= text.underscore %>.join(', ')}}</span> 
    </div> 
</div> 

我不确定这是在Rails + AngularJs组合中进行部分渲染的正确方法。不知道是否有更多角度的方式?谢谢!

+0

似乎确定,只是avoir重复' <%= model %>。 <%= text %>' – apneadiving

回答

0

我会用一个助手来产生的,而不是直接在HTML写的输入,像:

def input_for_model(model_name, text, options = { }) # I actually have no idea what is doing your input, you can probably find a better name 
    options = { id: "#{model_name}.#{text}", name: "#{model_name}.#{text}", 
       type: 'text', class: 'form-control', 
       ng-class: "{'text-danger': true}", 
       ng-model: "#{model_name}.#{text}" 
      }.merge(options) 
    tag(:input, options) 
end 

而且使用这样的:

<div class="form-group" ng-class="{'has-error': errors.<%= text.underscore %>}"> 
    <label for="<%= model %>.<%= text %>" class="col-xs-3 control-label"><%= text.titleize.humanize %></label> 
    <div class="col-xs-9"> 

    <%= input_for_model(model, text, type: local_assigns[:type] || 'text') %> 

    <span class="help-block" ng-show="errors.<%= text.underscore %>"><%= text.titleize.humanize %> {{errors.<%= text.underscore %>.join(', ')}}</span> 
    </div> 
</div> 
相关问题