,如果我有一個模型,如下所示:在Rails中,如何驗證特定控制器操作的模型?
class Post < ActiveRecord::Base
validate :content, presence: true, on: :post_create_action
end
class PostsController < ApplicationController
def create
@post = Post.new(post_params)
if @post.validate(:post_index_action) && @post.save
redirect_to post_path(@post)
end
end
end
我知道,因爲我在代碼中所描述的@ post.validate不工作,但我不知道這是否可能在軌道。
那麼,它應該像描述的那樣工作。只有你需要在那裏傳遞':post_create_action',我想。 –