gpt4 book ai didi

ruby-on-rails-3 - 使用浅层路由时,不同的路由需要不同的 form_for 参数

转载 作者:行者123 更新时间:2023-12-03 07:03:45 24 4
gpt4 key购买 nike

我在这里使用简单表单,但这也是普通 Rails 表单的问题。使用浅层路由时,form_for 需要不同的参数,具体取决于使用的上下文。

示例:为了编辑 (http://localhost:3000/notes/2/edit),_form.html.erb 需要有 simple_form_for(@note) 。但要创建新注释(http://localhost:3000/customers/2/notes/new)_form.html.erb 需要 simple_form_for([@customer, @note])。如果其中任何一个接收到错误的参数,我将收到“找不到方法”错误。

处理这个问题的最佳方法是什么?

  • 我可以制作两个单独的表单,但这看起来很困惑。
  • 我必须为反向链接设置@customer,但我可以在表单中使用不同的变量(例如@customer_form),而不是在编辑和更新方法中设置它,但这不一致并且有点令人困惑,因为我必须在新方法中设置 @customer_form 和 @customer。
  • 我可以像 this guy 那样将表单拆分到多个文件中。到目前为止,它看起来是最好的选择,但我不太喜欢它,因为你不能只打开 _form.html.erb 并查看发生了什么。

这些是我唯一的选择吗?

示例如下:

config/routes.rb

Billing::Application.routes.draw do
resources :customers, :shallow => true do
resources :notes
end
end

耙动路线 | grep 注释

    customer_notes GET    /customers/:customer_id/notes(.:format)         notes#index
POST /customers/:customer_id/notes(.:format) notes#create
new_customer_note GET /customers/:customer_id/notes/new(.:format) notes#new
edit_note GET /notes/:id/edit(.:format) notes#edit
note GET /notes/:id(.:format) notes#show
PUT /notes/:id(.:format) notes#update
DELETE /notes/:id(.:format) notes#destroy

app/views/notes/_form.html.erb

#                      v----------------------------- Right here
<%= simple_form_for (@note), html: { class: 'form-vertical'} do |f| %>
<%= f.input :content %>

<%= f.button :submit %>
<% end -%>

app/views/notes/new.html.erb

<h1>New note</h1>

<%= render 'form' %>

<%= link_to 'Back', customer_path(@customer) %>

app/views/notes/edit.html.erb

<h1>Editing note</h1>

<%= render 'form' %>

<%= link_to 'Show', @note %>
<%= link_to 'Back', customer_path(@customer) %>

app/controllers/notes_controller.rb

class NotesController < ApplicationController

def show
@note = Note.find(params[:id])
@customer = Customer.find(@note.customer_id)

respond_to do |format|
format.html
format.json {render json: @note }
end
end

# GET /notes/new
# GET /notes/new.json
def new
@note = Note.new
@customer = Customer.find(params[:customer_id])

respond_to do |format|
format.html # new.html.erb
format.json { render json: @note }
end
end

# GET /notes/1/edit
def edit
@note = Note.find(params[:id])
@customer = Customer.find(@note.customer_id)
end

# POST /notes
# POST /notes.json
def create
@customer = Customer.find(params[:customer_id])
@note = @customer.notes.build(params[:note])

respond_to do |format|
if @note.save
format.html { redirect_to @customer, notice: 'Note was successfully created.' }
format.json { render json: @note, status: :created, location: @note }
else
format.html { render action: "new" }
format.json { render json: @note.errors, status: :unprocessable_entity }
end
end
end

# PUT /notes/1
# PUT /notes/1.json
def update
@note = Note.find(params[:id])
@customer = Customer.find(@note.customer_id)

respond_to do |format|
if @note.update_attributes(params[:note])
format.html { redirect_to @customer, notice: 'Note was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @note.errors, status: :unprocessable_entity }
end
end
end

# DELETE /notes/1
# DELETE /notes/1.json
def destroy
@note = Note.find(params[:id])
@note.destroy

respond_to do |format|
format.html { redirect_to :back }
format.json { head :no_content }
end
end
end

最佳答案

如果传递给表单构建器的数组中的第一个对象是nil,Rails 将仅 POST 到第二个对象。因此,不要在 Controller 的编辑操作中设置 @customer 对象。如果您需要访问客户对象,请通过@note调用。

如果您对新建和编辑使用相同的部分,则需要在 Controller 的新操作中设置 @note.customer (@customer won'编辑时无需设置)。

我认为这就是 Rails 团队希望它发挥作用的方式。

关于ruby-on-rails-3 - 使用浅层路由时,不同的路由需要不同的 form_for 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9772588/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com