ruby on rails自定义路由按钮

时间:2013-04-28 06:59:03

标签: ruby-on-rails routes

我正在尝试找出如何在点击时创建link_to或button_to,触发我已添加到控制器的以下操作

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 => '+@@@@',
                    :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

rake路线的输出 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"}

2 个答案:

答案 0 :(得分: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

让我知道它是否有帮助,或者你是否再次遇到同样的错误。

答案 1 :(得分:0)

您已将控制器操作命名为send_notification(单数),但在使用复数send_notifications的路线中。您应该选择其中一个,并将它们命名为相同。