假设有Project
的表单页面,其中包含name
和description
。看看render :partial
。
<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 :(得分:0)
我会使用Helper来生成输入,而不是直接在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>