【问题标题】:Duplicate an entry in Rails from show page从显示页面复制 Rails 中的条目
【发布时间】:2018-01-15 16:20:59
【问题描述】:

show.html.erb 页面,我想在该页面上创建记录的副本并发送到new.html.erb 页面。 Rails dup 方法可以做到这一点(并且没有 ID 或创建日期),但我不知道如何到达那里。

我目前遇到的错误是:Couldn't find Street with 'id'= 指向streets_controller.rb 中的@street = Street.find(params[:id]).dup 行。 (此页面已更新,修复了我遇到的一些问题。谢谢评论者。)

我的show.edit.erb 页面的一部分:

<p>
  <strong>Previous Street Name:</strong>
  <%= @street.previous_name %>
</p>

<p>
  <strong>&lsquo;Current/Next&rsquo; Street Name:</strong>
  <%= @street.current_name %>
</p>

<p>
  <strong>Earliest Date for &lsquo;Previous&rsquo; Name:</strong>
  <%= @street.date_earliest %>
</p>

<p>
  <strong>Latest Date for &lsquo;Previous&rsquo; Name:&nbsp;&nbsp;</strong>
  <%= @street.date_latest %>
</p>

<p>
  <strong>Entry covers from cross street to cross street or similar:</strong>
  <%= @street.cross_streets %>
</p>
<p>
  <strong>Approximate Number of Blocks:</strong>
  <%= @street.number_of_blocks %>
</p>

<p>
  <strong>Length of segment:</strong>
  <%= @street.extent_length.round(2) %> miles
</p>

然后是同一页面上的一些按钮:

<button type="button" class="btn btn-outline-primary" >
  <%= link_to 'Edit', edit_street_path(@street) %>
</button>

<button type="button" class="btn btn-outline-primary" >
  <%= link_to 'New Historic Street Entry', new_street_path %>
</button>

<%= link_to 'Duplicate This Entry', dup_street_path, :action => "dup", :id => @street.id, class: "btn btn-outline-primary" %> 

最后一个按钮是我完成复制的第一次尝试。现在的问题在于传递 id。

而在streets_controller.rb

def dup
  @street = Street.find(params[:id]).dup
  render :new
end

我终于搞定了路线:get 'dup_street', to: 'streets#dup'

为了完整起见:new.html.erb

<p id="notice"><%= notice %></p>
<% provide(:title, 'New Historic Street Name') %>

<%= render 'form_edit', street: @street %>

<br/>
<div class="btn btn-primary active">
  <%= link_to 'To List of Streets', streets_path %>
</div>

<%= link_to 'Duplicate This Entry', dup_street_path, :action => "dup", :id => @street.id, class: "btn btn-outline-primary" %> 

_form_edit.html.erb 是:

<div class="container-fluid">
  <p>This is for a street name from Previous Name/Earliest Date to Current Name/Latest Date</p>
    <div class="row"> <!-- Setting up to have two columns -->
       <div class="col-4"> <!--Left hand column. -->
        <%= form_with(model: street, local: true) do |form| %>
          <% if street.errors.any? %>
            <div id="error_explanation">
              <h2><%= pluralize(street.errors.count, "error") %> prohibited this street from being saved:</h2>
              <ul>
              <% street.errors.full_messages.each do |message| %>
                <li><%= message %></li>
              <% end %>
              </ul>
            </div>
          <% end %>

          <div class="form-inputs">
            <div class="field">
              <%= form.label "City" %>
              <%= form.text_field :city, id: :street_city %>
            </div>

            <div class="field">
              <%= form.label "Previous Name" %>
              <%= form.text_field :previous_name, id: :street_previous_name %>
            </div>

            <div class="field">
              <%= form.label "Current Street Name" %>
              <%= form.text_field :current_name, id: :street_current_name, placeholder: "Name at end of period (Latest confirmed date)", :size=>"95%" %> 
            </div>

            <div class="row">
              <div class="col-lg-12 text-center font-bold">For Previous Street Name</div>
              <div class="field col-lg-6">
                <%= form.label "Earliest Confirmed Date" %>
                <%= form.text_field :date_earliest, id: :street_date_earliest %>
              </div>

              <div class="field col">
                <%= form.label "Latest Confirmed Date" %>
                <%= form.text_field :date_latest, id: :street_date_latest %>
              </div>
            </div>

              <div class="field">
                <%= form.label "Entry covers from cross street to cross street or similar" %>
                <%= form.text_area :cross_streets, id: :street_cross_streets, :maxlength=>"100%" %>
              </div>

            <div class="row">
              <div class="col-lg-12 text-center font-bold">Extent of This Item</div>
              <div class="field col-lg-6">
                <%= form.label "Approximate Number of Blocks" %>
                <%= form.text_field :number_of_blocks, id: :street_number_of_blocks %>
              </div>

              <div class="field col">
                <%= form.label "Length of segment, miles" %>
                <%= form.text_field :extent_length, id: :street_extent_length %>
              </div>
            </div>

            <div class="field">
              <%= form.label "Reference 1" %>
              <%= form.text_area :ref1, id: :street_ref1, :maxlength=>"95%" %>
            </div>

            <div class="field">
              <%= form.label "Reference 2" %>
              <%= form.text_area :ref2, id: :street_ref2 %>
            </div>

            <div class="field">
              <%= form.label "Reference 3" %>
              <%= form.text_area :ref3, id: :street_ref3 %>
            </div>

            <div class="field">
              <%= form.label :notes %>
              <%= form.text_area :notes, id: :street_notes %>
            </div>

            <div class="field">
              <%= form.label "Extent as JSON [lng lat]" %>
              <%= form.text_field :extent_json, id: :street_extent_json, placeholder: "Will be automagically filled in when line is drawn!" %>
            </div>
            <% if @street.extent_array? %>
              <div class="field">
                <%= form.label "Extend as string (array lat lng)This column is replaced by the one above and will be removed." %>
                <%= form.text_area :extent_array, id: :street_extent_array, placeholder: "Not currently being used except for pre-existing entries." %>
              </div>
             <% end %>
            <div class="actions">
              <%= form.submit "Create/Update Historical Street", class: "btn btn-primary" %>
            </div>
          </div> <!-- end form-inputs -->
        <% end %> <!-- End of form -->
       </div> <!-- end col-4, the left hand column -->

       <!-- The next two columns -->
       <%= render 'map_and_control_and_draw' %> <!-- middle column with map -->
       <%= render 'overlaymap_selector' %>   <!-- the third column for selector basemap -->

  </div> <!-- end row -->

</div>  <!-- end container-fluid -->

这似乎是一个常见的请求,但在这里和其他地方搜索并查看我的几本 Rails 书籍;我只找到有关 Rails 2 或 3 的内容,它们不足以让我理解如何在 Rails 5 中实现这一点。重申当前的错误是:Couldn't find Street with 'id'= 以及来自better_errors 的以下内容:

Request info
Request parameters: {"controller"=>"streets", "action"=>"dup"}
Rack session: (object too large. Modify ActionDispatch::Request::Session#inspect or increase BetterErrors.maximum_variable_inspect_size)
Local Variables: <blank>
Instance Variables:
@_action_has_layout: true
@_routes: nil
@_request: (object too large. Modify ActionDispatch::Request#inspect or increase BetterErrors.maximum_variable_inspect_size)
@_response: (object too large. Modify ActionDispatch::Response#inspect or increase BetterErrors.maximum_variable_inspect_size)
@_lookup_context: #<ActionView::LookupContext:0x00007fc0be919408 @details_key=nil, @cache=true, @prefixes=["streets", "application"], @rendered_format=nil, @details={:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder]}, @view_paths=#<ActionView::PathSet:0x00007fc0be919318 @paths=[#<ActionView::OptimizedFileSystemResolver:0x00007fc0bb607358 @pattern=":prefix/:action{.:locale,}{.:formats,}{+:variants,}{.:handlers,}", @cache=#<ActionView::Resolver::Cache:0x7fc0bb607330 keys=10 queries=0>, @path="/Users/gscar/Documents/Croatian Restaurants Project-CroRes/LA Historical Street Names/la_hist_street/app/views">]>>
@_action_name: "dup"
@_response_body: nil
@marked_for_same_origin_verification: true
@_config: {}
@current_user: #<User id: 4, name: "Example User", email: "example@railstutorial.org", created_at: "2017-06-09 23:06:24", updated_at: "2017-06-09 23:06:24", password_digest: <bunch of numbers>", remember_digest: nil, admin: true, activation_digest: "$2a$10...", activated: true, activated_at: "2017-06-09 23:06:24", reset_digest: nil, reset_sent_at: nil>
@_params: <ActionController::Parameters {"controller"=>"streets", "action"=>"dup"} permitted: false>

【问题讨论】:

  • dup函数末尾添加render :new
  • @Mahmoud 说谢谢。我按照您的建议进行了更新,并将代码更新为一个按钮,以使评论更容易。但我最终得到了一个空白的new 页面。
  • 可以添加新页面源代码吗?
  • 检查从 link_to 助手生成的到 dup 页面的路由是否正确。将其发布在您的答案中。我认为路径是错误的
  • @Pravav Singhal。你是对的,我的路线错了。我已更新,现在开始使用 dup 方法,但出现新错误:Couldn't find Street with 'id'=。我猜没有id 被通过。似乎请求先到路由,然后到 dup 方法。

标签: ruby-on-rails methods routes rails-activerecord


【解决方案1】:

您拨打dup时没有street_params

试试这个

def dup
  @street = Street.find(params[:id]).dup
  render :new
end

【讨论】:

  • 史蒂夫特钦。谢谢,但没有帮助。我将更新我的代码以显示这一点,因为这似乎是朝着正确方向迈出的一步。 @赛义德。我添加了代码。
  • @Gerg 也将 &lt;%= link_to 'Duplicate This Entry', new_street_path, :action =&gt; "dup", :id =&gt; @street.id %&gt; 更改为 &lt;%= link_to 'Duplicate This Entry',controller: 'streets', :action =&gt; "dup", :id =&gt; @street.id %&gt; 并且不要忘记在您的路线中添加此操作。
  • @Mahmoud Sayed controller: 'streets' 引起了其他问题,尽管我已经改变了可能做同样事情的路线。但我也在我的错误消息中发现了这个@_params &lt;ActionController::Parameters {"controller"=&gt;"streets", "action"=&gt;"dup"} permitted: false&gt;
  • 那么您收到的错误消息究竟是什么
  • 这一行出现错误:`@street = Street.find(params[:id]).dup` 错误是:ActiveRecord::RecordNotFound at /dup_street Couldn't find Street with 'id'=1 然后在另一部分调试@ 987654329@
【解决方案2】:

一旦我知道如何表达我正在寻找的东西,我就找到了Rails - passing parameters in link_to,这导致了最终的link_to

&lt;%= link_to 'Duplicate This Entry', dup_street_path(id: @street.id), action: "dup", class: "btn btn-outline-primary" %&gt;

但是@Steve Turczyn 为@street = Street.find(params[:id]).dup 和@Mahmoud Sayed 为render :new 提供了让我到达那里的部分。谢谢大家。

【讨论】:

    猜你喜欢
    • 2017-12-17
    • 2013-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-11
    • 2012-06-09
    • 1970-01-01
    相关资源
    最近更新 更多