Reputation: 11299
I am trying to figure out how to write the show action for Form Objects.
The following is code taken from RailsCast episode 416 project
app/forms/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
app/controllers/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
I don't see how one would pass in the ID of the model and specify the variables.
Does anyone have an example or can provide one using the episode's code as a starting point?
Upvotes: 0
Views: 223
Reputation: 3911
That particular episode abstracts the form building into a separate folder in the app path. You have go into the views folder to see how it is being used. Specifically, here.
This is where all of the variables are set that are passed as part of the sessions/signup
routes that are defined in the routes.rb file.
However, a typical show
action would not have a form as it usually would display the information pertaining to the record queried. The edit
action is used to display the form and pass to the update
action. In that form, you would have the fields for the user information and you would provide a hidden_field :id
helper that would indicate the id of the user being updated. That, or use the routes parameter and instantiate it in the update
action prior to saving the changes from the passed params
hash.
That particular project does not have edit/update actions in it though.
Upvotes: 1