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.
would add a path to controller method to Send notification. So you just answered yourself.send_notifmethod from view ?send_notif) then you have to make a request from your client to server. That being said, you are misunderstanding about the MVC.