【问题标题】:dynamic path in form partial?部分形式的动态路径?
【发布时间】:2018-05-29 01:49:11
【问题描述】:

我将如何在表单部分中使用动态路径,该路径可以设置为不同的路径,具体取决于我在哪个页面中调用表单部分?

例如,在我的 _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 中,我尝试将“取消”设置为路径。

<%= render "form", action: "Create", cancel: root_path %>

这样做的正确方法是什么?它似乎在我的浏览器中工作,但在我的测试中我得到未定义的局部变量或方法“取消”。

ActionView::Template::Error: undefined local variable or method `cancel' for #<#<Class:0x00000001da1e38>:0x0000000322cf10>

【问题讨论】:

    标签: ruby-on-rails ruby forms partials


    【解决方案1】:

    你很亲密。您只需将cancel 指定为locals 的一部分即可将其通过部分:

    <%= render "form", action: "Create", locals: { cancel: root_path } %>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-12
      • 2015-03-30
      • 2017-08-10
      • 1970-01-01
      • 2015-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多