嵌套路由 - rails 5.2.4 - 未定义的局部变量或方法
Nested routes - rails 5.2.4 - Undefined local variable or method
为了在我的应用程序中为客户注册地址,我使用了嵌套路由,但是当单击 link 注册新地址或编辑现有地址时,系统会出现错误:
#<##Class:0x00007fd1c815fa58>:0x00007fd1d05ef598> 的未定义局部变量或方法“地址”
你的意思? @地址.
哪位好心人能告诉我错误在哪里,或者我还没有看到的缺失?
申请代码如下:
routes.rb
resources :clients do
resources :addresses
end
models/addresses.rb
class Address < ApplicationRecord
belongs_to :client
end
models/clients.rb
class Client < ApplicationRecord
has_many :addresses
end
controllers/addresses_controller.rb
class Backoffice::AddressesController < BackofficeController
before_action :set_client
before_action :set_address, only: [:edit, :update, :destroy]
def new
@address = @client.addresses.build
end
def create
@address = @client.addresses.build(params_address)
if @address.save
redirect_to backoffice_addresses_path, notice: "Endereço cadastrado com sucesso!"
else
render :new
end
end
def edit
end
def update
if @address.update(params_address)
redirect_to backoffice_client_addresses_path(@client), notice: "#{@client.name} alterado com sucesso!"
else
render :new
end
end
private
def set_client
@client = Client.find(params[:client_id])
end
def set_address
@address = @client.addresses.find(params[:id])
end
def params_address
params.require(:address).permit(:nickname,
:cep,
:street,
:number,
:district,
:city,
:state,
:client_id)
end
end
views/addresses/index.html.erb
<%= link_to new_backoffice_client_address_path(@client)" do %>
Novo
<% end %>
<% @client.addresses.each do |address| %>
<tr>
<td><%= address.nickname %></td>
<td> ... </td>
<%= link_to edit_backoffice_client_address_path(@client, address) do %>
Editar
<% end %>
<% end %>
views/addresses/edit.html.erb
<%= render partial: "addresses/shared/form" %>
views/addresses/shared/_form.html.erb
<%= form_with(model: [@client, address], local: true) do |f| %>
...
<% end %>
<%= form_with(model: @address, url: [@client, @address], local: true) do |f| %>
…
<% end %>
根据文档,url 的值“类似于传递给 url_for 或 link_to 的值”,因此使用数组应该有效。
这也适用于浅嵌套,因为数组被压缩了。
为了在我的应用程序中为客户注册地址,我使用了嵌套路由,但是当单击 link 注册新地址或编辑现有地址时,系统会出现错误:
#<##Class:0x00007fd1c815fa58>:0x00007fd1d05ef598> 的未定义局部变量或方法“地址” 你的意思? @地址.
哪位好心人能告诉我错误在哪里,或者我还没有看到的缺失?
申请代码如下:
routes.rb
resources :clients do
resources :addresses
end
models/addresses.rb
class Address < ApplicationRecord
belongs_to :client
end
models/clients.rb
class Client < ApplicationRecord
has_many :addresses
end
controllers/addresses_controller.rb
class Backoffice::AddressesController < BackofficeController
before_action :set_client
before_action :set_address, only: [:edit, :update, :destroy]
def new
@address = @client.addresses.build
end
def create
@address = @client.addresses.build(params_address)
if @address.save
redirect_to backoffice_addresses_path, notice: "Endereço cadastrado com sucesso!"
else
render :new
end
end
def edit
end
def update
if @address.update(params_address)
redirect_to backoffice_client_addresses_path(@client), notice: "#{@client.name} alterado com sucesso!"
else
render :new
end
end
private
def set_client
@client = Client.find(params[:client_id])
end
def set_address
@address = @client.addresses.find(params[:id])
end
def params_address
params.require(:address).permit(:nickname,
:cep,
:street,
:number,
:district,
:city,
:state,
:client_id)
end
end
views/addresses/index.html.erb
<%= link_to new_backoffice_client_address_path(@client)" do %>
Novo
<% end %>
<% @client.addresses.each do |address| %>
<tr>
<td><%= address.nickname %></td>
<td> ... </td>
<%= link_to edit_backoffice_client_address_path(@client, address) do %>
Editar
<% end %>
<% end %>
views/addresses/edit.html.erb
<%= render partial: "addresses/shared/form" %>
views/addresses/shared/_form.html.erb
<%= form_with(model: [@client, address], local: true) do |f| %>
...
<% end %>
<%= form_with(model: @address, url: [@client, @address], local: true) do |f| %>
…
<% end %>
根据文档,url 的值“类似于传递给 url_for 或 link_to 的值”,因此使用数组应该有效。
这也适用于浅嵌套,因为数组被压缩了。