Help us understand the problem. What is going on with this article?

rails4による記述法(3.2)との違い(備忘録)

More than 5 years have passed since last update.

cotnroller

params記法の違い

これまでは、params[:id]としていたところの記述をprivate関数に置き換えて書き換え
→ def article_params
params.require(:article).permit(:title, :text)
end

params.require(:article).permit(:title, :text)ここが違っているところがポイント

find(params[:id])がうまく機能しない

show,destroy,editなどのparamsでidを渡す場合

railsでhas_manyで関連しているモデルで親モデルのデータを消したら関連するモデルのデータも連動して消したいとき

:dependent => :destroy or :delete_all (1段階の関連であれば、:delete_all 2段階の関連であれば、:destroy )

show

viewで各メソッドへ移動する時の注意点

paramsを渡す場合、特に前でeach文などをまわしている場合は、どんな変数名で渡しているのかでえ間違っているときがある
ex. @user each do |u| でまわしているのに user_path(@user) で渡していたり・・・この場合は、 user_path(u)で渡す

jun910
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした