我的应用程序中有一个显示用户列表的表。
使用link_to
方法,我希望能够单击用户名,存储该用户的特定参数以及current_user
(已登录的用户)。
在表中单击的用户名应保存它的:id参数以及current_user
的ID。
将current_user
路由到的页面显示一个表单,该表单一旦提交,就应创建一个新的Booking
对象,将通过text_area
传递给notes
的值分配给:notes
字段,并且在提交后应分别将user
和current_user
的:id参数分配给Booking模型上名为cleaner_id
和host_id
的两个字段
这就是我尝试过的,
在routes.rb文件中:
get 'show_booking_form/:id', to: 'bookings#show', as: 'booking_form'
这应将用户引导至预订表。预订表格标题应显示表格用户的姓名。
以下是显示表用户的表字段:
<td><%= link_to "#{user.name}", booking_form_path(@user) %></td>
单击后,需要存储两个参数
1. current_user
ID,(执行点击操作的登录用户)(主机用户)
2. user
ID,(单击表用户)(清洁用户)
这是预订控制器show
的操作,它定义了主机和清洁工
def show
@host = current_user
@cleaner = User.find(params[:id])
end
这是用户被路由到的页面,其中包含以更干净的用户名作为标题的表单。此表单应分别以host_id
和cleaner_id
的形式提交current_user和table用户参数。
<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>
答案 0 :(得分:1)
在您的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
中。booking_form
就是为了new_booking_path
而已。同样,您不需要create_booking_path
,因为您已经有了bookings_path
,当您create
表单时,它将路由到您的POST
操作。通过此操作,单击链接时,您应该在:cleaner_id
操作的参数中看到:host_id
和new
。
然后,在您的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
的{{1}}操作并在其中可用。您可能需要稍微修改一下这种语法。
老实说,我不知道为什么当create
总是解析为host
时,您会做所有host
的事情。但是,我确定您有您的理由。
我想如果我是你,我会做的:
current_user
哪个会给您:
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 %>
动作中使用cleaner_id
,并且可以将create
用作current_user