将:id参数附加到GET请求并在视图中返回User

时间:2019-06-21 18:54:56

标签: ruby-on-rails ruby parameters routing

我的应用程序中有一个显示用户列表的表。

使用link_to方法,我希望能够单击用户名,存储该用户的特定参数以及current_user(已登录的用户)。

在表中单击的用户名应保存它的:id参数以及current_user的ID。

current_user路由到的页面显示一个表单,该表单一旦提交,就应创建一个新的Booking对象,将通过text_area传递给notes的值分配给:notes字段,并且在提交后应分别将usercurrent_user的:id参数分配给Booking模型上名为cleaner_idhost_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_idcleaner_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>

1 个答案:

答案 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-它可能​​是一个,也可能是另一个,并且很可能会导致您提到其中的错误@usernil时的注释)。
  • 无需在user.name上使用字符串插值
  • new_booking_path(或任何路径)中包含的额外键/值对,作为查询参数附加到url,并显示在params中。
  • 尝试使用标准的RESTful路由。 booking_form就是为了new_booking_path而已。同样,您不需要create_booking_path,因为您已经有了bookings_path,当您create表单时,它将路由到您的POST操作。

通过此操作,单击链接时,您应该在:cleaner_id操作的参数中看到:host_idnew

然后,在您的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