从显示页面复制 Rails 中的条目 [英] Duplicate an entry in Rails from show page

查看:43
本文介绍了从显示页面复制 Rails 中的条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

<strong>以前的街道名称:</strong><%=@street.previous_name %></p><p><strong>&lsquo;当前/下一个&rsquo;街道名称:</strong><%=@street.current_name %></p><p><strong>&lsquo;上一个&rsquo的最早日期名称:<%=@street.date_earliest %></p><p><strong>最新日期为&lsquo;上一个&rsquo;名称:&nbsp;&nbsp;</strong><%=@street.date_latest %></p><p><strong>条目涵盖从十字路口到十字路口或类似的:</strong><%=@street.cross_streets %></p><p><strong>大概的块数:</strong><%=@street.number_of_blocks %></p><p><strong>段的长度:</strong><%=@street.extent_length.round(2)%>英里</p>

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

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

streets_controller.rb

def dup@street = Street.find(params[:id]).dup渲染:新结尾

我终于找到了路线:get 'dup_street', to: 'streets#dup'

为了完整性:new.html.erb

<%= notice %>

<% provide(:title, '新的历史街道名称') %><%= 渲染 'form_edit',街道:@street %><br/><div class="btn btn-primary active"><%= link_to '到街道列表',street_path %>

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

_form_edit.html.erb 是:

<p>这是从以前的名称/最早日期到当前名称/最新日期的街道名称</p><div class="row"><!-- 设置为有两列--><div class="col-4"><!--左侧栏.--><%= form_with(model: street, local: true) 做 |form|%><% if street.errors.any?%><div id="error_explanation"><h2><%=pluralize(street.errors.count, "error") %>禁止保存这条街道:</h2><ul><% street.errors.full_messages.each do |message|%><li><%=消息%></li><%结束%>

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

<div class="field"><%= form.label "以前的名字" %><%= form.text_field :previous_name, id: :street_previous_name %>

<div class="field"><%= form.label "当前街道名称" %><%= form.text_field :current_name, id: :street_current_name, placeholder: "Name at the end of period (Latest Confirmed Date)", :size=>"95%" %>

<div class="row"><div class="col-lg-12 text-center font-bold">对于以前的街道名称</div><div class="field col-lg-6"><%= form.label最早确认日期"%><%= form.text_field :date_earliest, id: :street_date_earliest %>

<div class="field col"><%= form.label "最新确认日期" %><%= form.text_field :date_latest, id: :street_date_latest %>

<div class="field"><%= form.label "条目涵盖从十字路口到十字路口或类似的"%><%= form.text_area :cross_streets, id: :street_cross_streets, :maxlength=>"100%" %>

<div class="row"><div class="col-lg-12 text-center font-bold">此项的范围</div><div class="field col-lg-6"><%= form.label "近似块数" %><%= form.text_field :number_of_blocks, id: :street_number_of_blocks %>

<div class="field col"><%= form.label "线段长度,英里" %><%= form.text_field :extent_length, id: :street_extent_length %>

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

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

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

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

<div class="field"><%= form.label "Extent as JSON [lng lat]" %><%= form.text_field :extent_json, id: :street_extent_json, placeholder: "画线时会自动填充!"%>

<% 如果@street.extent_array?%><div class="field"><%= form.label "扩展为字符串 (array lat lng) 这一列被上面的一列替换,将被删除."%><%= form.text_area :extent_array, id: :street_extent_array, placeholder: 除了预先存在的条目,当前没有被使用."%>

<%结束%><div class="actions"><%= form.submit "创建/更新历史街",类:"btn btn-primary" %>

<!-- 结束表单输入--><%结束%><!-- 表格结束-->

<!-- 结束第 4 列,左侧列--><!-- 接下来的两列--><%=渲染'map_and_control_and_draw'%><!-- 带地图的中间列--><%= 渲染 'overlaymap_selector' %><!-- 选择器底图的第三列-->

<!-- 结束行-->

<!-- 结束容器流体-->

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

请求信息请求参数:{"controller"=>"streets", "action"=>"dup"}机架会话:(对象太大.修改 ActionDispatch::Request::Session#inspect 或增加 BetterErrors.maximum_variable_inspect_size)局部变量:<blank>实例变量:@_action_has_layout: 真@_routes:无@_request: (对象太大.修改ActionDispatch::Request#inspect或增加BetterErrors.maximum_variable_inspect_size)@_response: (对象太大.修改ActionDispatch::Response#inspect或增加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街道名称/la_hist_street/app/views">]>>@_action_name:重复"@_response_body:无@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: <一堆数字>", 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"} 允许:false>

解决方案

在您调用 dup

时没有 street_params

试试这个

def dup@street = Street.find(params[:id]).dup渲染:新结尾

From the show.html.erb page I want to create a copy of the record on that page and be sent to the new.html.erb page. Rails dup method does that (and doesn't have and id or creation date), but I can't figure out how to get there.

The error I'm currently getting is this: Couldn't find Street with 'id'= pointing at the @street = Street.find(params[:id]).dup line in the streets_controller.rb. (This page has been updated with fixes to some of the problems I had. Thank you commenters.)

Part of my show.edit.erb page:

<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>

And then some buttons on the same page:

<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" %> 

The last button is my first stab at getting the dup done. The problem is now in passing the id.

And in the streets_controller.rb

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

I finally got the route working: get 'dup_street', to: 'streets#dup'

And for completeness: 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 is:

<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 -->

Seems like this would be a common request, but searching here and elsewhere and looking in my several Rails books; I only find things about Rails 2 or 3, and they don't go quite for enough for me to understand how to make that work in Rails 5. To reiterate the current error is: Couldn't find Street with 'id'= and the following from 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>

解决方案

There are no street_params at the time you're calling dup

Try this

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

这篇关于从显示页面复制 Rails 中的条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆