Backbone model save

*爱你&永不变心* 提交于 2020-01-21 03:45:10

问题


I'm using rails and learning Backbone.

Help me please. Is there any way to set right model on save? When I create, for example, new user with some params and trying save it into the database, - on server side I have not user as object, but user fields in params. And only way to save user - is to set properties manually:

user = User.new(:login => params[:login], :password => params[:password]).save!

Is there any way which will generate real user model object (like a form_for generates)? I think actions described below I will need to use when I will update user and so on.

Thanks!


回答1:


There is a paramRoot 'parameter'. You say rails - so I'll assume coffee script:

class User extends Backbone.Model
  paramRoot: 'user'
<url or some other stuff>



回答2:


The 'paramRoot' parameter is used by the backbone_rails_sync adapter that backbone-rails gem provides. That's why you can't find it in backbone's website.

Another way, if you are not using the gem and it's adapter, is to render the user with :root => false

render json: @user, root: false


来源:https://stackoverflow.com/questions/8016296/backbone-model-save

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!