redirect_to @model て何よ
railsでscaffoldすると、controllerに redirect_to @modelってのが出来て来ます。
こんな感じ。
def create @model = Model.new(params[:model]) respond_to do |format| if @model.save format.html { redirect_to @model, notice: "model was successfully created."} format.json { render json: @model, status: :created, location: @model } else format.html { render action: "new" } format.json { render json: @model.errors, status: :unprocessable_entity } end end end
本読んでも特に解説も無くしれっと同じように書いてあるか、:actionや:methodを使ったり、URL明示する書き方にしているかのどっちかです。
例えばこんな感じ。
redirec_to "/models/#{@model.id}
これは、ディフォルトだとshowを呼び出すルーティングですが、scaffoldが作ったまま(redirect_to @model)の状態で新しいエントリをcreateしても同じようにshowにルーティングされます。
> rake routes .... model GET /models/:id(.:format) models#show ....
というわけで、"redirect_to @model" は結局以下と同じ意味のようです。
redirect_to model_url(id: @modle.to_param) # または redirect_to model_url(id: @model.id)
CoCも全体がわかってくると便利なんでしょうけど、それまでがそれまでが大変だ...