将 :id 参数附加到 GET 请求并返回视图中的用户
Attaching :id param to a GET request and returning User in view
我的应用程序中有一个 table 显示用户列表。
使用 link_to
方法,我希望能够单击用户名,存储该用户的特定参数以及 current_user
(已登录的用户)。
在 table 中单击的用户名应该保存它的 :id 参数以及 current_user
的 ID。
current_user
路由到的页面显示一个表单,提交后应创建一个新的 Booking
object 分配通过 text_area
传入的值notes
到 :notes
字段,并且应该将 user
和 current_user
的 :id 参数分配给预订模型上的两个字段,称为 cleaner_id
和 host_id
分别在提交时。
这是我试过的,
在 routes.rb 文件中:
get 'show_booking_form/:id', to: 'bookings#show', as: 'booking_form'
这应该将用户路由到预订表格。预订表格标题应显示 table 用户名。
这是显示 table 用户的 table 字段:
<td><%= link_to "#{user.name}", booking_form_path(@user) %></td>
单击后,需要存储两个参数
1. current_user
id,(点击的登录用户)(主机用户)
2. user
id,(被点击的 table 用户)(清洁用户)
这是定义房东和清洁工的预订控制器 show
操作
def show
@host = current_user
@cleaner = User.find(params[:id])
end
这是用户被路由到的页面,其中包含一个以清洁用户名称作为标题的表单。此表单应将 current_user 和 table 用户参数分别提交为 host_id
和 cleaner_id
。
<h1 style='text-align:center'>Book <%= @cleaner.name %> for a cleaning </h1>
<%= bootstrap_form_for create_booking_path 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 %>
</div>
在你的 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
(或任何路径)中包含的额外 key/value 对作为查询参数附加到 url 并将显示在您的 params
.
- 尝试使用标准 RESTful 路线。当
new_booking_path
的用途时,几乎不需要 booking_form
。同样,您不需要 create_booking_path
,因为您已经有了 bookings_path
,当您 POST
您的表单时,它将路由到您的 create
操作。
这样,当您单击 link 时,您应该会在 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
操作并在其中可用。您可能需要 fiddle 使用该语法。
老实说,当 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
我的应用程序中有一个 table 显示用户列表。
使用 link_to
方法,我希望能够单击用户名,存储该用户的特定参数以及 current_user
(已登录的用户)。
在 table 中单击的用户名应该保存它的 :id 参数以及 current_user
的 ID。
current_user
路由到的页面显示一个表单,提交后应创建一个新的 Booking
object 分配通过 text_area
传入的值notes
到 :notes
字段,并且应该将 user
和 current_user
的 :id 参数分配给预订模型上的两个字段,称为 cleaner_id
和 host_id
分别在提交时。
这是我试过的,
在 routes.rb 文件中:
get 'show_booking_form/:id', to: 'bookings#show', as: 'booking_form'
这应该将用户路由到预订表格。预订表格标题应显示 table 用户名。
这是显示 table 用户的 table 字段:
<td><%= link_to "#{user.name}", booking_form_path(@user) %></td>
单击后,需要存储两个参数
1. current_user
id,(点击的登录用户)(主机用户)
2. user
id,(被点击的 table 用户)(清洁用户)
这是定义房东和清洁工的预订控制器 show
操作
def show
@host = current_user
@cleaner = User.find(params[:id])
end
这是用户被路由到的页面,其中包含一个以清洁用户名称作为标题的表单。此表单应将 current_user 和 table 用户参数分别提交为 host_id
和 cleaner_id
。
<h1 style='text-align:center'>Book <%= @cleaner.name %> for a cleaning </h1>
<%= bootstrap_form_for create_booking_path 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 %>
</div>
在你的 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
(或任何路径)中包含的额外 key/value 对作为查询参数附加到 url 并将显示在您的params
. - 尝试使用标准 RESTful 路线。当
new_booking_path
的用途时,几乎不需要booking_form
。同样,您不需要create_booking_path
,因为您已经有了bookings_path
,当您POST
您的表单时,它将路由到您的create
操作。
这样,当您单击 link 时,您应该会在 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
操作并在其中可用。您可能需要 fiddle 使用该语法。
老实说,当 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