【问题标题】:Attaching :id param to a GET request and returning User in view将 :id 参数附加到 GET 请求并返回用户视图
【发布时间】:2019-06-21 18:54:56
【问题描述】:

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

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

在表格中被点击的用户名应该保存它的 :id 参数以及current_user 的 id。

current_user 被路由到的页面显示一个表单,该表单提交后应创建一个新的Booking 对象,将通过称为notestext_area 传入的值分配给: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 和表用户参数。

<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>

【问题讨论】:

  • 有什么问题?
  • 我收到以下错误:没有路线匹配 {:action=>"show", :controller=>"bookings", :id=>nil},缺少必需的键: [:id] 。尝试运行服务器时......我猜我处理参数的方式存在问题

标签: ruby-on-rails ruby parameters routing


【解决方案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 中。
  • 尝试使用标准的 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 现在是隐藏字段,将提交给您的 BookingControllercreate 操作并在其中可用。您可能需要稍微修改一下该语法。

老实说,当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

【讨论】:

    猜你喜欢
    • 2019-11-29
    • 1970-01-01
    • 2016-08-15
    • 2021-08-10
    • 2020-01-07
    • 2020-07-15
    • 1970-01-01
    • 1970-01-01
    • 2019-09-08
    相关资源
    最近更新 更多