Rails strong parameters 的参数传递问题

sensi · March 06, 2015 · Last by sensi replied at March 06, 2015 · 2297 hits

一个 product 有很多 articles,现在创建一个新的 article,需要把 product_id 和 article 的参数一同传到 article 的 controller,

我在网上始终没有找到传到 article 的 controller 的写法.(api 里面只是有一个例子传到 product 的 controller),

请问 strong parameters 这部分的 product_id 参数部分怎么写 就是这个部分:

def article_params
          params.require(:article).premit(:title, :content)
      end

下面是请求的参数

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"NjwASyACrLfTKqcmwSdwbWcTJlxcsrHGGmM5AoeLM2w=",
 "article"=>{"title"=>"hello",
 "content"=>"helllo"},
 "commit"=>"Create Article",
 "product_id"=>"1"}

def article_params
  params.require(:article).premit(:title, :content, :product_id)
end

f.hidden_field :product_id 写在 form_for or form_tag 之间,注意保证你的 article 模型有 product_id field.

#1 楼 @flowerwrong product_id 能传递了 但是提示了同样地错误 undefined method `premit' for {"title"=>"helo", "content"=>"helo", "product_id"=>"1"}:ActionController::Parameters

private
       def article_params
           params.require(:article).premit(:title, :content, :product_id)
end

#2 楼 @sensi 检查你们的拼写:should be permit not premit

#3 楼 @lyfi2003 糗大了 谢谢你!

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