从button_to调用控制器方法

时间:2014-05-20 14:42:36

标签: ruby-on-rails ruby button view controller

我想在控制器中调用此方法

  def setplaylist
   @stream.programmed_at = Time.now.to_i
  end

使用视图中的这一行

<%= button_to 'Reprogram', @stream, :method => :setplaylist, :remote => true %>

我得到一个没有路由匹配[POST]错误。

修改 我的routes.rb文件

   Rails.application.routes.draw do
  get 'splashpage/splash'
  root :to => 'splashpage#splash', :as =>'splash'


resources :streams do
  resources :videos

end

编辑2 佣金路线

         Prefix Verb   URI Pattern                                   Controller#Action
splashpage_splash GET    /splashpage/splash(.:format)                  splashpage#splash
           splash GET    /                                             splashpage#splash
    stream_videos GET    /streams/:stream_id/videos(.:format)          videos#index {:member=>{:setplaylist=>:post}}
                  POST   /streams/:stream_id/videos(.:format)          videos#create {:member=>{:setplaylist=>:post}}
 new_stream_video GET    /streams/:stream_id/videos/new(.:format)      videos#new {:member=>{:setplaylist=>:post}}
edit_stream_video GET    /streams/:stream_id/videos/:id/edit(.:format) videos#edit {:member=>{:setplaylist=>:post}}
     stream_video GET    /streams/:stream_id/videos/:id(.:format)      videos#show {:member=>{:setplaylist=>:post}}
                  PATCH  /streams/:stream_id/videos/:id(.:format)      videos#update {:member=>{:setplaylist=>:post}}
                  PUT    /streams/:stream_id/videos/:id(.:format)      videos#update {:member=>{:setplaylist=>:post}}
                  DELETE /streams/:stream_id/videos/:id(.:format)      videos#destroy {:member=>{:setplaylist=>:post}}
          streams GET    /streams(.:format)                            streams#index {:member=>{:setplaylist=>:post}}
                  POST   /streams(.:format)                            streams#create {:member=>{:setplaylist=>:post}}
       new_stream GET    /streams/new(.:format)                        streams#new {:member=>{:setplaylist=>:post}}
      edit_stream GET    /streams/:id/edit(.:format)                   streams#edit {:member=>{:setplaylist=>:post}}
           stream GET    /streams/:id(.:format)                        streams#show {:member=>{:setplaylist=>:post}}
                  PATCH  /streams/:id(.:format)                        streams#update {:member=>{:setplaylist=>:post}}
                  PUT    /streams/:id(.:format)                        streams#update {:member=>{:setplaylist=>:post}}
                  DELETE /streams/:id(.:format)                        streams#destroy {:member=>{:setplaylist=>:post}}

rake routes | grep setplaylist

       stream_videos GET    /streams/:stream_id/videos(.:format)          videos#index {:member=>{:setplaylist=>:post}}
                  POST   /streams/:stream_id/videos(.:format)          videos#create {:member=>{:setplaylist=>:post}}
 new_stream_video GET    /streams/:stream_id/videos/new(.:format)      videos#new {:member=>{:setplaylist=>:post}}
edit_stream_video GET    /streams/:stream_id/videos/:id/edit(.:format) videos#edit {:member=>{:setplaylist=>:post}}
     stream_video GET    /streams/:stream_id/videos/:id(.:format)      videos#show {:member=>{:setplaylist=>:post}}
                  PATCH  /streams/:stream_id/videos/:id(.:format)      videos#update {:member=>{:setplaylist=>:post}}
                  PUT    /streams/:stream_id/videos/:id(.:format)      videos#update {:member=>{:setplaylist=>:post}}
                  DELETE /streams/:stream_id/videos/:id(.:format)      videos#destroy {:member=>{:setplaylist=>:post}}
          streams GET    /streams(.:format)                            streams#index {:member=>{:setplaylist=>:post}}
                  POST   /streams(.:format)                            streams#create {:member=>{:setplaylist=>:post}}
       new_stream GET    /streams/new(.:format)                        streams#new {:member=>{:setplaylist=>:post}}
      edit_stream GET    /streams/:id/edit(.:format)                   streams#edit {:member=>{:setplaylist=>:post}}
           stream GET    /streams/:id(.:format)                        streams#show {:member=>{:setplaylist=>:post}}
                  PATCH  /streams/:id(.:format)                        streams#update {:member=>{:setplaylist=>:post}}
                  PUT    /streams/:id(.:format)                        streams#update {:member=>{:setplaylist=>:post}}
                  DELETE /streams/:id(.:format)                        streams#destroy {:member=>{:setplaylist=>:post}}

在进行Max更改后,我收到了无方法错误。 undefined方法setplaylist_stream_path。

1 个答案:

答案 0 :(得分:1)

您需要添加setplaylist路线。在您的路线文件中,更改此

resources :streams do
  resources :videos
end

resources :streams, :member => {:setplaylist => :post} do
  resources :videos
end

现在你的button_to(我认为默认生成一个帖子请求)应该可行。最好将语法更改为:

<%= button_to 'Reprogram', setplaylist_stream_path(@stream), :remote => true %>

您还需要确保在您的操作代码运行时定义@stream。您也可能希望在更改后保存@stream。将您的操作更改为

def setplaylist
  @stream = Stream.find(params[:id])
  @stream.programmed_at = Time.now.to_i
  @stream.save
end

如果这就是它所做的一切,我实际上只是在打电话给通常的更新&#39;行动,而不是为它做出新的行动。