新手问题 让 user 关联 record

tonytonyjan · November 26, 2012 · Last by fengzhiquxiang replied at March 06, 2015 · 1845 hits

在 controller 里面各位会选择这样写:

def new
  @post = current_user.posts.build
end

然后在 form 里面加个 hidden input

或者

def create
  @post = current_user.posts.build(params[:post])
end

只是好奇:)

应该推荐第二种吧 第一种存在安全隐患,比如手动修改表单隐藏字段 user_id,这样就可以操作其它用户的资源了

@post = current_user.posts.build 这个是怎么实现的,怎么看 rails 源码?

You need to Sign in before reply, if you don't have an account, please Sign up first.