我无法使用link_to在控制器中调用操作。
我的routes.rb文件中定义了一条路由到预订控制器(详见下文),我想访问'my / reservations'控制器中的'create_commercial'动作。
因此,如果我使用rake routes | grep 'reservation'
visitor_my_reservations GET (/:locale)/my/reservations/visitor(.:format) {:locale=>/en|en-US|en-GB/, :controller=>"my/reservations", :action=>"visitor"}
create_commercial_my_reservations POST (/:locale)/my/reservations/create_commercial(.:format) {:locale=>/en|en-US|en-GB/, :controller=>"my/reservations", :action=>"create_commercial"}
confirm_my_reservation PUT (/:locale)/my/reservations/:id/confirm(.:format) {:locale=>/en|en-US|en-GB/, :controller=>"my/reservations", :action=>"confirm"}
pay_my_reservation GET (/:locale)/my/reservations/:id/pay(.:format) {:locale=>/en|en-US|en-GB/, :controller=>"my/reservations", :action=>"pay"}
payment_success_my_reservation GET (/:locale)/my/reservations/:id/payment_success(.:format) {:locale=>/en|en-US|en-GB/, :controller=>"my/reservations", :action=>"payment_success"}
payment_cancel_my_reservation GET (/:locale)/my/reservations/:id/payment_cancel(.:format) {:locale=>/en|en-US|en-GB/, :controller=>"my/reservations", :action=>"payment_cancel"}
my_reservations POST (/:locale)/my/reservations(.:format) {:locale=>/en|en-US|en-GB/, :controller=>"my/reservations", :action=>"create"}
new_my_reservation GET (/:locale)/my/reservations/new(.:format) {:locale=>/en|en-US|en-GB/, :controller=>"my/reservations", :action=>"new"}
edit_my_reservation GET (/:locale)/my/reservations/:id/edit(.:format) {:locale=>/en|en-US|en-GB/, :controller=>"my/reservations", :action=>"edit"}
my_reservation GET (/:locale)/my/reservations/:id(.:format) {:locale=>/en|en-US|en-GB/, :controller=>"my/reservations", :action=>"show"}
PUT (/:locale)/my/reservations/:id(.:format) {:locale=>/en|en-US|en-GB/, :controller=>"my/reservations", :action=>"update"}
DELETE (/:locale)/my/reservations/:id(.:format) {:locale=>/en|en-US|en-GB/, :controller=>"my/reservations", :action=>"destroy"}
show_test_my_reservations GET /test/my/reservations/show(.:format) {:controller=>"test/my/reservations", :action=>"show"}
new_test_my_reservation GET /test/my/reservations/new(.:format) {:controller=>"test/my/reservations", :action=>"new"}
所以我试着: -
<td><%= link_to 'book', create_commercial_my_reservations_path %></td>
我也试试这个: -
<%= link_to('Book', {:controller => 'my/reservations', :action => 'create_commercial', :method => :post}) %>
我在控制台窗口中看到两个选项: -
Started GET "/en-GB/my/reservations/create_commercial" for 127.0.0.1 at Sat Jan 05 18:29:30 +0000 2013
Processing by My::ReservationsController#show as HTML
Parameters: {"id"=>"create_commercial", "locale"=>"en-GB"}
Geokit is using the domain: localhost
网址是对的,但是正在调用操作'show',方法是'get',而不是'post'。
有没有人知道我可能做错了什么......非常感谢指导。
答案 0 :(得分:1)
...试
<%= link_to 'book', create_commercial_my_reservations_path, :method => :post %>
有一点需要注意,链接到#create动作而不是#new动作似乎有点奇怪,但我不知道你的应用......