开发者

Rails controller when no ActiveRecord model?

开发者 https://www.devze.com 2022-12-25 16:25 出处:网络
I\'m trying to create a rails web app that does not use ActiveRecord framework as I have no relational database back end.

I'm trying to create a rails web app that does not use ActiveRecord framework as I have no relational database back end.

So far I managed to create a simple example application that works perfectly for listing, showing and editing my records.

However I'm facing some problems when it comes to the record creation.

The issue is that the controller class does not behave in the same manner if the model class does not inherit from ActiveRecord::Base.

From the controller's point of view, in a classic active record model schema where the model inherits from ActiveRecord::Base, the creation of a record seems to follow this sequence :

  • controller.new : creation of a non persistent record, rendering the input form
  • controller.create : set up the record wit开发者_StackOverflow社区h user input, persistant storage , ….

However in the case where the model is not a ActiveRecord we get the following sequence :

  • controller.new
  • controller.update

Now of course this is a little bit strange because we're skipping the creation method so it will lead to a nil instance error !

I'm pretty certain that this behavior is dictated by something that should be implemented in the model, however I was unable to find what (probably because I'm an absolute beginner with rails )

So if anyone here has some pointers on what I should be looking/implementing in order to solve this situation and have the same behaviour in both cases, I'd be realy grateful.

Thanks,


Have your custom model implement the MyModel#new_record? method (instance method) and you will have the desired behavior. When new_record? returns true, it will go to create, and when it returns false it will go to update.

0

精彩评论

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