2013-04-28 82 views
0

我試圖找出如何使的link_to或button_to點擊觸發的時候,我已經加入到我的控制器Ruby on Rails的用於按鈕

def send_notification 
    @event = Event.find(params[:id]) 
    @users = User.where(:team_id => current_user[:team_id]).all 
    @account_sid = '@@@@' 
    @auth_token = '@@@@'# your authtoken here 
    @client = Twilio::REST::Client.new(@account_sid, @auth_token) 
    @account = @client.account 

    if @event.update_attributes(params[:event]) 
     @users.each do |u| 
     #Starts the SMS process 
     #Uncomment to enable the SMS process 
     team = truncate(u.team.name, :length => 15, :omission => '') 
     opponent = truncate(@event.opponent.name, :length => 10, :omission => '') 
     date = @event.datetime.strftime("%a, %e %b, %Y") 
     time = @event.datetime.to_s(:event_time) 
     location = truncate(@event.location.name, :length => 15, :omission => '') 
     if u.mobile 
      @message = @account.sms.messages.create({ 
        :from => '[email protected]@@@', 
        :to => "+#{phone_number(u.mobile, :Australia)}", 
        :body => "#{u.first_name}: #{team} v #{opponent} #{date} #{time}@ #{location}" }) 
      puts @message 
     end 
     @availability = u.availabilities.update(:unique_id => Base64.encode64("#{u.id.to_s}_#{@event.id.to_s}_#{@event.team.id.to_s}")) 
     Notifier.event_added(u,@event).deliver 
     end 
    end 
    end 

以下動作的定製路線,我已經添加了下面我的路由

resources :events do 
    post 'send_notifications' 
    get 'page/:event_page', :action => :index, :on => :collection 
    end 

耙路線的輸出 event_send_notifications POST /events/:event_id/send_notifications(.:format)

視圖代碼 =button_to 'Send Notifications' , event_send_notifications_path, :class => 'button'

錯誤 No route matches {:action=>"send_notifications", :controller=>"events"}

回答

1

將您的控制器操作重命名爲send_notifications
你的行爲需要一些參數,所以你也應該提供它們。然後,使用這種方式

=button_to('Send Notifications' , send_notifications_event_path(params), :action => 'send_notifications' , :class => 'button') 

這裏param是參數的哈希值,你的動作需要。

同時,讓你的動作的RESTful作爲

resources :events do 
    post 'send_notifications', :on => :member 
    get 'page/:event_page', :action => :index, :on => :collection 
end 

或者讓我知道,如果它幫助,如果你再次得到同樣的錯誤。

+0

現在得到'沒有路由匹配{:action =>「show」,:controller =>「events」,:id =>「27」}' – 2013-04-28 07:27:12

+0

驗證'rake routes'中的路由路徑。另外,嘗試'發送'send_notifications',:on =>:member'一次。 – kiddorails 2013-04-28 07:30:01

+0

path from'event_send_notifications POST /events/:event_id/send_notifications(.:format)events#send_notifications' – 2013-04-28 07:32:15

0

你命名你的控制器動作send_notification(單數),但在航線使用複數send_notifications。你應該選擇一個或另一個,並將它們命名爲相同。

+0

已更新,仍然收到相同的錯誤 – 2013-04-28 07:11:46

+0

您是否重新啓動了服務器? – 2013-04-28 07:12:20

+0

重新啓動服務器,仍顯示相同的錯誤 – 2013-04-28 07:16:04