例如,我在 RoR 4 中有一个控制器 PostController。其中的操作:
def index
@posts = Post.all.order('created_at DESC')
end
def new
@post = Post.new #second question
end
def create
@post = Post.new(post_params)
post.save
redirect_to @post #first question
end
def show
@post = Post.find(params[:id])
end
private
def post_params
params.require(:post).permit(:title, :body)
end
所以,第一个问题是:为什么我们写的redirect_to @post
时候会自动重定向到show/id
?rails 怎么知道,它必须去show
查看?
第二个问题:为什么我必须@post = Post.new
在新动作中写?当我评论它时,我也可以创建一个Post
. 什么是尊重写在new
行动或错过它?
我是 Rails 新手,里面有很多魔法