简体   繁体   中英

How to call a model method from rails view through a controller?

Please bear with me. I went through similar questions here but my requirement is quiet different.

I have a model called RideLaterRequest . In the app I am allowing the user to create a record on this model. And currently once the record is saved a notification is being sent to a android app. The logic is as follows:

ride_later_request.rb

class RideLaterRequest < ActiveRecord::Base

   after_save :send_notif #run send_notif method once the record is saved.

   def send_notif
      RideLaterRequestHandler.ride_later_request_created(self)
   end

end

The above method actually calls another class method (the earlier developer defined a method in a separate class)

class RideLaterRequestHandler
    def self.ride_later_request_created(ride_later_request)
      #####................####
    end
end

So once this method executes a notification is being sent to the android app.

Now I am trying to stop this automated system. And handle this flow manually by adding a button in view file.

First I stopped the automated notification by commenting after_save :send_notif in the ride_later_request.rb file. So now there are no notifications being sent once a new record is saved on that model.

In a view file I am displaying the records of RideLaterRequest model as follows:

admin_reports_controller.rb

class AdminReportsController < ApplicationController

  def trip_report
   @ride_later_request = RideLaterRequest.all
  end

end

trip_report.html.erb

<table>
   <tr>
      <th>Date</th>
      <th>Trip ID</th>
      <th>Send Notification</th>
   </tr>
   <% @ride_later_requests.each do |trip_report| %>
   <tr>
      <td><%= trip_report.planned_start_time.to_date%></td>
      <td><%= trip_report.id%></td>
      <td>
       <%= button_to "Send Notification", #In normal case I would have added a path to the controller method to Send notification here %> 
      </td>
   </tr> 
</table>

As you can see I am trying add a button action where it sends the notification. The only thing I am trying to do here is, earlier the notifications were being sent once a new ride_later_request row was created. Now I am stopping that and trying to do it manually on a already saved record by adding a button. My question is is how can I call def send_notif model method that I have explained above in the beginning on this button that I am trying to give ?

I know this is app specific question but I have tried my best to make it rails specific by explaining it, I guess. Help is much appreciated.

How about creating a send_notification action on your RideLaterRequestController and then (in routes.rb ) setting up a send_nofication_ride_later_request path. Perhaps something like:

resources :ride_later_requests do 
  member do 
    post :send_nofication
  end
end

Which yields:

send_nofication_ride_later_request POST   /ride_later_requests/:id/send_nofication(.:format)   ride_later_requests#send_nofication
                                   POST   /ride_later_requests(.:format)                       ride_later_requests#create
               ride_later_requests GET    /ride_later_requests(.:format)                       ride_later_requests#index
            new_ride_later_request GET    /ride_later_requests/new(.:format)                   ride_later_requests#new
           edit_ride_later_request GET    /ride_later_requests/:id/edit(.:format)              ride_later_requests#edit
                ride_later_request GET    /ride_later_requests/:id(.:format)                   ride_later_requests#show
                                   PATCH  /ride_later_requests/:id(.:format)                   ride_later_requests#update
                                   PUT    /ride_later_requests/:id(.:format)                   ride_later_requests#update
                                   DELETE /ride_later_requests/:id(.:format)                   ride_later_requests#destroy

Then do something like:

<%= link_to 'Send Notification', send_nofication_ride_later_request_path(trip_report), method: :post %>

You can dress that link to look like a button.

Then, in your RideLaterRequestController , something like:

class RideLaterRequestController < ApplicationController

  ...

  def send_notification
    @ride_later_request = RideLaterRequest.find_by(id: params[:id])
    RideLaterRequestHandler.ride_later_request_created(@ride_later_request)
    # redirect, render, or whatever depending on whether you want this 
    # to be AJAXy or regular HTML.
  end

  ...

end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM