我有一个包含大量属性的对象“设备”。编辑其中一个设备时,我想要一个复制按钮,按下该按钮将创建一个新设备并加载其“编辑页面”,其中旧设备参数填充到 text_field 中。但是我对如何使用按钮调用动作控制方法感到非常困惑,我目前收到一条错误消息
"没有路由匹配 {:action=>"clone", :controller=>"device", :id=>"1"}"
如果我可以调用“克隆”方法来工作,那么我认为我的状态会很好。任何帮助将不胜感激!我当前的代码如下:
编辑.html.erb
<div class="row">
<%= form_for(@device) do |f| %>
<div class="span3 offset0">
<%= f.label "Unit Name" %>
<%= f.text_field :unitName %>
.
.
.
<%= f.label "Router Terminal Server IP" %>
<%= f.text_field :routerTerminalServerIp %>
</div>
<div class="span3 offset0">
<%= f.label "N2x Server" %>
<%= f.text_field :n2xServer %>
.
.
.
<%= f.label "Last Changed On" %>
<%= f.text_field :updated_at %>
<%= f.label "Update, Copy, or Delete Device" %>
<%= f.submit "Update", class: "btn btn-medium btn-info" %>
<%= link_to "Clone", :controller => "device", :action => "clone" %>
<%= link_to "Delete", device_path, class: "btn btn-medium btn-danger" %>
</div>
<% end %>
</div>
路线.rb
App::Application.routes.draw do
resources :devices
root 'static_pages#home'
match 'devices/clone', to: 'devices#clone', via: 'get'
end
devices_controller.rb
class DevicesController < ApplicationController
def new
@device = Device.new
end
def clone
oldDevice = Device.find(params[:id])
@device = Device.new
@device = @oldDevice.dup
@device.save
redirect_to edit_device_path(@device.id)
end
def create
@device = Device.new(device_params)
@device.lastChangedBy = request.remote_ip
if @device.save
redirect_to edit_device_path(@device.id)
else
render 'new'
end
end
def show
@device = Device.find(params[:id])
end
def edit
@device = Device.find(params[:id])
end
def update
@device = Device.find(params[:id])
@device.lastChangedBy = request.remote_ip
if @device.update_attributes(device_params)
redirect_to edit_device_path(@device)
else
render "edit"
end
def destroy
Device.find(params[:id]).destroy
redirect_to root_url
end
end
private
def device_params
params.require(:device).permit(:unitName, ...., :owner)
end
end