0

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.

5
  • I'm not sure I can understand your question correctly. You already know that you would add a path to controller method to Send notification. So you just answered yourself. Commented Dec 8, 2017 at 18:44
  • @yeuem1vannam Yes. I would have if the method was in controller. But its a model method that I need to add a path to. So how can I call that send_notif method from view ? Commented Dec 8, 2017 at 18:51
  • Did you check out my answer? You don't call methods from a rendered view. Rendered views submit requests to controllers (roughly) which resolve via routes to controller actions. Then, controllers take it from there. Commented Dec 8, 2017 at 18:55
  • @user3576036 When you see the HTML ( the button ) on your browser, there is no models / controllers, just client / server. So if your business has to be done on the server ( the send_notif ) then you have to make a request from your client to server. That being said, you are misunderstanding about the MVC. Commented Dec 8, 2017 at 18:58
  • I think I am asking the question wrong way. I know the controller makes a call to the model. But inorder to do that I need to add a path to that method in controller right ? Am I right ? Commented Dec 8, 2017 at 19:00

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Let me try this. Instead of creating a controller I can define that method in the existing controller right ? I already have a controller called admin_reports in which I am listing the records of RideLaterRequest model. So I can add the method you mentioned there itself right ?
Sure. I was just riffing. TBH, if I were you, I would try to stay RESTful about the whole thing and create a RideLaterRequestNotificationsController and use the create action to do your thing. But, hey, whatever floats your boat.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.