提问人:Jens Schmidt 提问时间:11/17/2019 更新时间:11/17/2019 访问量:30
has_many form_with中未定义的方法“networkinterface_path”
Undefined method `networkinterface_path' in a has_many form_with
问:
我从几个小时开始搜索,因为我找不到错误。我有同样的情况(使用 ipaddress 模型),它工作得很好。也许有人可以解释为什么会发生这种情况以及我如何解决它。
app\views\routinginformations_form.html.erb:
<% flash.each do |key, value| %>
<div class="<%= flash_class(key) %>">
<%= value %>
</div>
<% end %>
<%= form_with(model: [@networkinterface, @routinginfo], local: true) do |form| %>
<div class="form-group">
<%= form.label :metric %>
<%= form.text_field :metric, class: "form-control" %>
</div>
<div class="form-group">
<%= form.label :networkobject %>
<%= form.collection_select(:networkobject_id, Networkobject.all, :id, :name, {include_blank: "#{t("routinginfo.selectno")}"}, { class: "form-control" }) %>
</div>
<div class="form-group">
<%= form.label :gateway %>
<%= form.collection_select(:gateway, Systemobject.all, :id, :hostname, {include_blank: "#{t("routinginfo.selectgw")}"}, { class: "form-control" }) %>
</div>
<%= form.submit class: "btn btn-primary" %>
<% end %>
在提交时,它给了我这个错误:
jensrailsdev | Started POST "/networkinterfaces/1/routinginformations" for 192.168.23.20 at 2019-11-16 15:00:15 +0000
jensrailsdev | (0.1ms) SELECT sqlite_version(*)
jensrailsdev | Processing by RoutinginformationsController#create as HTML
jensrailsdev | Parameters: {"authenticity_token"=>"m9oTCgcZyf6a8Y6rj2na0Subvn3Wju+FCRnmAiDlV+kRwPFLZLmN4vZfxkDa6SksT5ZfT4DX8IKTLPu4157qAw==", "routinginformation"=>{"metric"=>"574", "networkobject_id"=>"1", "gateway"=>"1"}, "commit"=>"Create", "networkinterface_id"=>"1"}
jensrailsdev | Networkinterface Load (0.1ms) SELECT "networkinterfaces".* FROM "networkinterfaces" WHERE "networkinterfaces"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
jensrailsdev | ↳ app/controllers/routinginformations_controller.rb:48:in `fetch_networkinterface'
jensrailsdev | Systemobject(#69889442002280) expected, got "1" which is an instance of String(#47418300423640)
jensrailsdev | Rendering routinginformations/new.html.erb within layouts/application
jensrailsdev | Systemobject Load (0.1ms) SELECT "systemobjects".* FROM "systemobjects" WHERE "systemobjects"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
jensrailsdev | ↳ app/views/routinginformations/new.html.erb:6
jensrailsdev | Customer Load (0.1ms) SELECT "customers".* FROM "customers" WHERE "customers"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
jensrailsdev | ↳ app/views/routinginformations/new.html.erb:6
jensrailsdev | Rendered routinginformations/_form.html.erb (Duration: 126.8ms | Allocations: 99817)
jensrailsdev | Rendered routinginformations/new.html.erb within layouts/application (Duration: 147.2ms | Allocations: 120123)
jensrailsdev | Completed 500 Internal Server Error in 166ms (ActiveRecord: 3.3ms | Allocations: 136558)
jensrailsdev |
jensrailsdev |
jensrailsdev |
jensrailsdev | ActionView::Template::Error (undefined method `networkinterface_path' for #<#<Class:0x00007f20cebd7a90>:0x00007f20cebd5970>):
jensrailsdev | 3: <%= value %>
jensrailsdev | 4: </div>
jensrailsdev | 5: <% end %>
jensrailsdev | 6: <%= form_with(model: [@networkinterface, @routinginfo], local: true) do |form| %>
jensrailsdev | 7: <div class="form-group">
jensrailsdev | 8: <%= form.label :metric %>
jensrailsdev | 9: <%= form.text_field :metric, class: "form-control" %>
jensrailsdev |
jensrailsdev | app/views/routinginformations/_form.html.erb:6
jensrailsdev | app/views/routinginformations/new.html.erb:13
jensrailsdev | app/controllers/routinginformations_controller.rb:15:in `rescue in create'
jensrailsdev | app/controllers/routinginformations_controller.rb:5:in `create'
jensrailsdev | app/controllers/application_controller.rb:6:in `switch_locale'
应用程序\视图\路由信息\new.html.erb:
<h1><%= t("routinginfo.newfor", name: "#{@networkinterface.name}" + " (" + "#{@networkinterface.aliasname}" + ")") %></h1>
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item"><%= link_to t(:home), root_path %></li>
<li class="breadcrumb-item"><%= link_to t("cust", name: @networkinterface.systemobject.customer.name), customer_path(@networkinterface.systemobject.customer) %></li>
<li class="breadcrumb-item"><%= link_to t("system", name: @networkinterface.systemobject.hostname), customer_path(@networkinterface.systemobject.customer) %></li>
<li class="breadcrumb-item"><%= link_to t("networkinterfaces.titleof", name: @networkinterface.name), networkinterface_ipaddresses_path(@networkinterface) %></li>
<li class="breadcrumb-item active" aria-current="page"><%= t("routinginfo.new") %></li>
</ol>
</nav>
<%= render 'form' %>
config\routes.rb:
Rails.application.routes.draw do
resources :systemobjects, only: [] do
resources :networkinterfaces
end
resources :networkinterfaces, only: [] do
resources :ipaddresses
resources :routinginformations
end
get 'main/index'
root 'main#index'
end
app\models\routinginformation.rb:
class Routinginformation < ApplicationRecord
belongs_to :gateway, class_name: 'Systemobject', foreign_key: :systemobject_id
belongs_to :networkobject
belongs_to :networkinterface
end
app\models\networkinterface.rb:
class Networkinterface < ApplicationRecord
belongs_to :systemobject
has_many :routinginformations
has_many :ipaddresses
end
app\controllers\routinginformations_controller.rb
class RoutinginformationsController < ApplicationController
before_action :fetch_networkinterface
before_action :set_routinginfo, only: [ :edit, :update, :destroy ]
def create
console
@routinginfo = @networkinterface.routinginformations.build(routinginfo_params)
@routinginfo.save!
flash[:success] = t('messages.succnew', name: 'unknown')
redirect_to systemobject_networkinterfaces_path(@networkinterface.systemobject.id)
rescue => e
flash.now[:error] = t('messages.errnew', msg: e.to_s)
logger.error(e.to_s)
render 'new'
end
def new
console
@routinginfo = @networkinterface.routinginformations.build
console
end
def show; end
def update
@routinginfo.update!(routinginfo_params)
flash[:success] = t('messages.succedit', name: 'unknown')
redirect_to systemobject_networkinterfaces_path(@ni.systemobject.id)
rescue => e
flash[:error] = t('messages.errnew', msg: e.to_s)
render 'edit'
end
def edit
end
def destroy
@routinginfo.destroy
flash[:success] = t('messages.succdelete', name: 'unknown')
redirect_to networkinterfaces_path(@ni.systemobject.id)
end
private
def fetch_networkinterface
@networkinterface = Networkinterface.find(params[:networkinterface_id])
end
def set_routinginfo
@routinginfo = @networkinterface.routinginformations.find(params[:id])
end
def routinginfo_params
params.require(:routinginformation).permit(:metric,
:networkinterface_id,
:networkobject_id,
:gateway)
end
end
我正在从以下位置调用新方法:app\views\networkinterfaces\index.html.html.erb:
[...]
<% if @sysobj.networkinterfaces.present? %>
<% @sysobj.networkinterfaces.each do |n| %>
[...]
<%= link_to new_networkinterface_routinginformation_path(n.id), class: "btn btn-secondary" do %>
<%= t(:new) %>
<%= mdi_tag("lan 20px") %>
<% end %></h2>
[...]
我不明白,他在哪里以及为什么叫“networkinterface_path”。ipaddresses 也是如此:
jensrailsdev | Started GET "/networkinterfaces/1/ipaddresses/new" for 192.168.23.20 at 2019-11-16 17:00:27 +0000
jensrailsdev | Processing by IpaddressesController#new as HTML
jensrailsdev | Parameters: {"networkinterface_id"=>"1"}
jensrailsdev | Networkinterface Load (0.2ms) SELECT "networkinterfaces".* FROM "networkinterfaces" WHERE "networkinterfaces"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
jensrailsdev | ↳ app/controllers/ipaddresses_controller.rb:44:in `get_ni'
jensrailsdev | Rendering ipaddresses/new.html.erb within layouts/application
jensrailsdev | Systemobject Load (0.1ms) SELECT "systemobjects".* FROM "systemobjects" WHERE "systemobjects"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
jensrailsdev | ↳ app/views/ipaddresses/new.html.erb:6
jensrailsdev | Customer Load (0.1ms) SELECT "customers".* FROM "customers" WHERE "customers"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
jensrailsdev | ↳ app/views/ipaddresses/new.html.erb:6
jensrailsdev | Rendered ipaddresses/_form.html.erb (Duration: 1.6ms | Allocations: 1091)
jensrailsdev | Rendered ipaddresses/new.html.erb within layouts/application (Duration: 5.4ms | Allocations: 2881)
jensrailsdev | Completed 200 OK in 25ms (Views: 15.9ms | ActiveRecord: 0.8ms | Allocations: 24340)
jensrailsdev |
jensrailsdev |
jensrailsdev | Started POST "/networkinterfaces/1/ipaddresses" for 192.168.23.20 at 2019-11-16 17:00:40 +0000
jensrailsdev | Processing by IpaddressesController#create as HTML
jensrailsdev | Parameters: {"authenticity_token"=>"KMfiwxQsUdUGcKIdYgICSDyYdgN/sEOdaHMpf/6UpN2qz9ZoJ+culi9oQCnObSMvlD45TXDXPRpqOeDkfOzLNA==", "ipaddress"=>{"ipv4address"=>"19.101.12.33", "ipv6address"=>"", "ipv4subnetmask"=>"24", "ipv6subnetmask"=>"", "externalip"=>"0", "natted"=>"0"}, "commit"=>"Create", "networkinterface_id"=>"1"}
jensrailsdev | Networkinterface Load (0.1ms) SELECT "networkinterfaces".* FROM "networkinterfaces" WHERE "networkinterfaces"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
jensrailsdev | ↳ app/controllers/ipaddresses_controller.rb:44:in `get_ni'
jensrailsdev | (0.1ms) begin transaction
jensrailsdev | ↳ app/controllers/ipaddresses_controller.rb:8:in `create'
jensrailsdev | Ipaddress Create (215.3ms) INSERT INTO "ipaddresses" ("ipv4address", "ipv6address", "ipv4subnetmask", "externalip", "natted", "networkinterface_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?) [["ipv4address", "19.101.12.33"], ["ipv6address", ""], ["ipv4subnetmask", 24], ["externalip", 0], ["natted", 0], ["
networkinterface_id", 1], ["created_at", "2019-11-16 17:00:40.421205"], ["updated_at", "2019-11-16 17:00:40.421205"]]
jensrailsdev | ↳ app/controllers/ipaddresses_controller.rb:8:in `create'
jensrailsdev | (18.5ms) commit transaction
jensrailsdev | ↳ app/controllers/ipaddresses_controller.rb:8:in `create'
jensrailsdev | Systemobject Load (0.1ms) SELECT "systemobjects".* FROM "systemobjects" WHERE "systemobjects"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
jensrailsdev | ↳ app/controllers/ipaddresses_controller.rb:10:in `create'
jensrailsdev | Redirected to http://10.23.8.2:3000/systemobjects/1/networkinterfaces
jensrailsdev | Completed 302 Found in 243ms (ActiveRecord: 234.2ms | Allocations: 5490)
jensrailsdev |
jensrailsdev |
jensrailsdev | Started GET "/systemobjects/1/networkinterfaces" for 192.168.23.20 at 2019-11-16 17:00:40 +0000
jensrailsdev | Processing by NetworkinterfacesController#index as HTML
jensrailsdev | Parameters: {"systemobject_id"=>"1"}
jensrailsdev | Systemobject Load (0.2ms) SELECT "systemobjects".* FROM "systemobjects" WHERE "systemobjects"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
jensrailsdev | ↳ app/controllers/networkinterfaces_controller.rb:40:in `get_sysobj'
jensrailsdev | Rendering networkinterfaces/index.html.erb within layouts/application
jensrailsdev | Customer Load (0.2ms) SELECT "customers".* FROM "customers" WHERE "customers"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
jensrailsdev | ↳ app/views/networkinterfaces/index.html.erb:9
jensrailsdev | Networkinterface Load (0.1ms) SELECT "networkinterfaces".* FROM "networkinterfaces" WHERE "networkinterfaces"."systemobject_id" = ? [["systemobject_id", 1]]
jensrailsdev | ↳ app/views/networkinterfaces/index.html.erb:20
jensrailsdev | Ipaddress Load (0.1ms) SELECT "ipaddresses".* FROM "ipaddresses" WHERE "ipaddresses"."networkinterface_id" = ? [["networkinterface_id", 1]]
jensrailsdev | ↳ app/views/networkinterfaces/index.html.erb:54
jensrailsdev | Routinginformation Load (0.1ms) SELECT "routinginformations".* FROM "routinginformations" WHERE "routinginformations"."networkinterface_id" = ? [["networkinterface_id", 1]]
jensrailsdev | ↳ app/views/networkinterfaces/index.html.erb:73
jensrailsdev | Rendered networkinterfaces/index.html.erb within layouts/application (Duration: 8.5ms | Allocations: 4981)
jensrailsdev | Completed 200 OK in 35ms (Views: 32.5ms | ActiveRecord: 0.8ms | Allocations: 19917)
答:
0赞
Jens Schmidt
11/17/2019
#1
我自己发现了错误。这不是一条错误的路径或路由的一些问题。将“systemobject”别名为“gateway”是个问题。我不知道我该怎么做,但是将“返回”app\models\routinginformation.rb 切换为:
class Routinginformation < ApplicationRecord
belongs_to :systemobject
belongs_to :networkobject
belongs_to :networkinterface
end
(也对应_form.html.erb中的collection_select),它“突然”起作用了。
评论
"#{@networkinterface.name}" + " (" + "#{@networkinterface.aliasname}" + ")"
"#{@networkinterface.name} (#{@networkinterface.aliasname})"