我如何在一个可以设置为不同路径的表单部分中使用动态路径,具体取决于我正在调用表单部分的页面?
例如,在我的_form.html.erb文件的底部,我希望能够根据我调用它的页面动态更改“取消”路径:
<div class="row">
<div class="col-md-6 offset-md-3">
<%= form_for(@chef, :html => {class: "form-horizontal", role: "form" }) do |f| %>
<div class="form-group">
<%= f.label :chefname %>
<%= f.text_field :chefname, class: "form-control",
placeholder: "Chef Name",
autofocus: true %>
</div>
<div class="form-group">
<%= f.label :email %>
<%= f.email_field :email, class: "form-control",
placeholder: "Email" %>
</div>
<div class="form-group">
<%= f.label :password %>
<%= f.password_field :password, class: "form-control",
placeholder: "Must be at least 5 characters" %>
</div>
<div class="form-group">
<%= f.label :password_confrimation, "Confirm Password" %>
<%= f.password_field :password_confrimation, class: "form-control",
placeholder: "Confirm Password" %>
</div>
<div class="form-group">
<%= f.submit "#{action} Account", class: "btn btn-primary" %>
</div>
<% end %>
<%= link_to "Cancel", cancel %>
<hr>
<% if logged_in? && (current_chef == @chef || current_chef.admin?) %>
<%= link_to "Delete Account", chef_path(@chef), method: :delete,
class: "btn btn-sm btn-outline-danger",
data: { confirm: "Are you sure you want to delete this account and all corresponding recipes?" } %>
<% end %>
</div>
</div>
然后在我正在调用它的new.html.erb中,我尝试将'cancel'设置为路径。
<%= render "form", action: "Create", cancel: root_path %>
这样做的正确方法是什么?它似乎在我的浏览器中工作,但在我的测试中我得到未定义的局部变量或方法'取消'。
ActionView::Template::Error: undefined local variable or method `cancel' for #<#<Class:0x00000001da1e38>:0x0000000322cf10>
答案 0 :(得分:2)
你非常接近。您只需指定cancel
作为locals
的一部分即可将其发布到部分内容:
<%= render "form", action: "Create", locals: { cancel: root_path } %>