我试图弄清楚如何为表单对象编写显示操作。
以下是从 RailsCast 第 416 集项目中获取的代码
应用程序/表格/signup_form.rb
class SignupForm
# Rails 4: include ActiveModel::Model
extend ActiveModel::Naming
include ActiveModel::Conversion
include ActiveModel::Validations
def persisted?
false
end
def self.model_name
ActiveModel::Name.new(self, nil, "User")
end
validates_presence_of :username
validate :verify_unique_username
validates_format_of :email, with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/
validates_length_of :password, minimum: 6
delegate :username, :email, :password, :password_confirmation, to: :user
delegate :twitter_name, :github_name, :bio, to: :profile
def user
@user ||= User.new
end
def profile
@profile ||= user.build_profile
end
#...
end
应用程序/控制器/users_controller.rb
class UsersController < ApplicationController
def new
@signup_form = SignupForm.new
end
def create
@signup_form = SignupForm.new
if @signup_form.submit(params[:user])
session[:user_id] = @signup_form.user.id
redirect_to @signup_form.user, notice: "Thank you for signing up!"
else
render "new"
end
end
def show
@user = current_user
end
end
我看不到如何传递模型的 ID 并指定变量。
有没有人有一个例子或者可以提供一个使用剧集代码作为起点的例子?