将数据从路径传递到构建多个对象的控制器

时间:2013-03-12 18:33:24

标签: ruby-on-rails ruby-on-rails-3

我一直在努力实现一个让我头疼几天的功能(rails newb)。

我有一个由用户组成的应用程序,以及安排特殊用户预订的功能。我在特殊用户个人资料页面上添加了“预约”链接,该页面应该创建(1)“预订”对象,以及(2)创建预订对象的子项(booking.user =特殊用户)。第二部分是以后只有预订。用户才能访问预订对象。

以下是我失败的尝试。

No route matches [GET] "/users/2/book_person"

耙路线:

book_person_user POST /users/:id/book_person(.:format)

路线:

resources :users do
 member do
  post :book_person, :controller => "bookings", :action => "book_person"
 end
end

Booking_controller

def book_person
@booking = Booking.new
@booking.name = "Booking with #{params[:id]}"
if @booking.save
  @user = User.find(params[:id])
    if not @user.participant_of?(@booking)
      @booking.users << @user
      flash[:notice] = 'User was successfully added to booking'
    else
      flash[:error] = 'User was already part of booking'
    end
    redirect_to @booking 
else
  flash[:notice] = "Could not add booking"
  redirect_to @booking
end

在用户个人资料页面上链接以预订该用户:

<%= link_to "Book Sessions", book_person_user_path, class: "btn btn-large" %>

1 个答案:

答案 0 :(得分:1)

试试这个:

<%= link_to "Book Sessions", book_person_user_path, :method => :post, class: "btn btn-large" %>