在您的routes.rb 中,只需执行以下操作:
resources :bookings
这会给你:
bookings GET /bookings(.:format) bookings#index
POST /bookings(.:format) bookings#create
new_booking GET /bookings/new(.:format) bookings#new
edit_booking GET /bookings/:id/edit(.:format) bookings#edit
booking GET /bookings/:id(.:format) bookings#show
PATCH /bookings/:id(.:format) bookings#update
PUT /bookings/:id(.:format) bookings#update
DELETE /bookings/:id(.:format) bookings#destroy
然后,做这个:
link_to "#{user.name}", booking_form_path(@user)
看起来更像:
link_to @user.name, new_booking_path(cleaner_id: @user.id, host_id: current_user.id)
几个注意事项:
- 这假设您有
@user 可供您使用(您同时引用了 user 和 @user - 它可能是其中之一,并且很可能是您收到评论中提到的错误的原因,如果 @user出于某种原因是nil)。
- 无需在
user.name 上使用字符串插值
-
new_booking_path(或任何路径,就此而言)中包含的额外键/值对作为查询参数附加到 url,并将显示在您的 params 中。
- 尝试使用标准的 RESTful 路由。当
new_booking_path 的用途是booking_form 时,几乎不需要booking_form。同样,您不需要create_booking_path,因为您已经有bookings_path,当您POST 表单时,它会路由到您的create 操作。
这样,当您单击链接时,您应该会在 new 操作的参数中看到 :cleaner_id 和 :host_id。
然后,在您的 new 操作中,执行以下操作:
class BookingsController < ApplicationController
def new
@cleaner = User.find(params[:cleaner_id])
@host = current_user
@booking = Booking.new
end
end
现在,在您的表单中,您可以执行以下操作:
<h1 style='text-align:center'>Book <%= @cleaner.name %> for a cleaning </h1>
<%= bootstrap_form_for @booking do |f| %>
<%= f.text_area :notes,
rows: 6,
minlength: 5,
maxlength: 1000,
placeholder: 'Leave a note for your cleaner!',
class: 'form-control' %>
<%= f.hidden_field :cleaner_id, value: @cleaner.id %>
<%= f.hidden_field :host_id, value: @host.id %>
<%= f.submit "Book Now!", id:'client-btn', class: 'form-control btn btn-primary' %>
<% end %>
@cleaner_id 和 @host_id 现在是隐藏字段,将提交给您的 BookingController 的 create 操作并在其中可用。您可能需要稍微修改一下该语法。
老实说,当host 总是解析为current_user 时,我不知道你为什么要做所有host 业务。但是,我相信你有你的理由。
我想如果我是你,我会这样做:
resources :cleaners do
resources :bookings, shallow: true
end
这会给你:
cleaner_bookings GET /cleaners/:cleaner_id/bookings(.:format) bookings#index
POST /cleaners/:cleaner_id/bookings(.:format) bookings#create
new_cleaner_booking GET /cleaners/:cleaner_id/bookings/new(.:format) bookings#new
edit_booking GET /bookings/:id/edit(.:format) bookings#edit
booking GET /bookings/:id(.:format) bookings#show
PATCH /bookings/:id(.:format) bookings#update
PUT /bookings/:id(.:format) bookings#update
DELETE /bookings/:id(.:format) bookings#destroy
cleaners GET /cleaners(.:format) cleaners#index
POST /cleaners(.:format) cleaners#create
new_cleaner GET /cleaners/new(.:format) cleaners#new
edit_cleaner GET /cleaners/:id/edit(.:format) cleaners#edit
cleaner GET /cleaners/:id(.:format) cleaners#show
PATCH /cleaners/:id(.:format) cleaners#update
PUT /cleaners/:id(.:format) cleaners#update
DELETE /cleaners/:id(.:format) cleaners#destroy
如果你不想要那些 cleaners 路径,那么就这样做:
resources :cleaners, only: [] do
resources :bookings, shallow: true
end
然后:
link_to @user.name, new_cleaner_booking_path(@user)
你的控制器:
class BookingsController < ApplicationController
def new
@cleaner = User.find(params[:cleaner_id])
@booking = Booking.new
end
end
还有你的表格:
<h1 style='text-align:center'>Book <%= @cleaner.name %> for a cleaning </h1>
<%= bootstrap_form_for [@cleaner, @booking] do |f| %>
<%= f.text_area :notes,
rows: 6,
minlength: 5,
maxlength: 1000,
placeholder: 'Leave a note for your cleaner!',
class: 'form-control' %>
<%= f.submit "Book Now!", id:'client-btn', class: 'form-control btn btn-primary' %>
<% end %>
现在您的create 操作中应该有cleaner_id,您可以使用current_user 作为您的host