开发者

Calling a method in Controller from Model, I know it's not right ...

but how else???

My Model is handling the logic and data for a booking system that I'm writing that allows users to make recurring bookings of resources. I want to show a pop up letting the user know that there was an issue with a recurring booking (conflict for one of the resources on one of the days for instance), but to also allow them to continue with the booking anyway, i.e don't just fail validation and rollback.

The logic is fine for this but what is the best way to call the pop up displaying conflicts if I 开发者_运维百科cannot/should not call a controller method from the model that fires some AJAX?

Using Rails 3

Thank for the help


I'd start by defining a method on the model which returns any conflicting events, e.g.:

class Event

   def conflicts
      Event.where(...)
   end

end

Then, in your view you can iterate through any conflicts if there are, and display them on the page.

If you want to do this before saving the event to the database, then you could add some javascript to the page to POST the event data via AJAX, along with a 'preview' param, and have the controller return a JSON representation of the event without saving it. Your javascript would then display any conflicts if there are any.

The controller might look something like this:

class EventsController

   def create
      @event = Event.new(params[:event])

      unless params[:preview]
         @event.save
      end

      respond_with @event, :include => :conflicts            
   end  
end

I'll leave the javascript to you! :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜