· 2 min read

Screencast: Mehrseitige Formulare mit Wicked

Downloads in verschiedenen Formaten:

mp4 m4v webm ogg

Resourcen:

terminal

[bash] rails g controller user_steps [/bash]

user_steps_controller.rb

[ruby] class UserStepsController < ApplicationController include Wicked::Wizard steps :personal, :social def show @user = current_user render_wizard end def update @user = current_user @user.attributes = params[:user] render_wizard @user end private

def redirect_to_finish_wizard redirect_to root_url, notice: “Thank you for signing up.” end end [/ruby]

user_steps/personal.html.erb

[html]

Tell us a little about yourself.

<%= render layout: ‘form’ do |f| %>

<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :date_of_birth %>
<%= f.date_select :date_of_birth, start_year: 1900, end_year: Date.today.year, include_blank: true %>
<%= f.label :bio %>
<%= f.text_area :bio, rows: 5 %>
<% end %> [/html]

user_steps/social.html.erb

[html]

Where can we find you?

<%= render layout: ‘form’ do |f| %>

<%= f.label :twitter_username %>
<%= f.text_field :twitter_username %>
<%= f.label :github_username %>
<%= f.text_field :github_username %>
<%= f.label :website %>
<%= f.text_field :website %>
<% end %> [/html]

user_steps/_form.html.erb

[html] <%= form_for @user, url: wizard_path do |f| %> <% if @user.errors.any? %>

<%= pluralize(@user.errors.count, “error”) %> prohibited this user from being saved:

    <% @user.errors.full_messages.each do |msg| %>
  • <%= msg %>
  • <% end %>
<% end %> <%= yield f %>
<%= f.submit “Continue” %> or <%= link_to “skip this step”, next_wizard_path %>
<% end %> [/html]

users/new.html.erb

[html]

Sign Up

<%= form_for @user do |f| %> <% if @user.errors.any? %>

<%= pluralize(@user.errors.count, “error”) %> prohibited this user from being saved:

    <% @user.errors.full_messages.each do |msg| %>
  • <%= msg %>
  • <% end %>
<% end %>

<%= f.label :email %>
<%= f.text\_field :email %>
<%= f.label :password %>
<%= f.password\_field :password %>
<%= f.label :password\_confirmation %>
<%= f.password\_field :password\_confirmation %>
<%= f.submit "Sign Up" %>
<% end %> \[/html\]

users_controller.rb

[ruby] def create @user = User.new(params[:user]) if @user.save session[:user_id] = @user.id redirect_to user_steps_path else render :new end end [/ruby]

models/user.rb

[ruby] validates_format_of :twitter_username, without: /W/, allow_blank: true # validates_presence_of :twitter_username, if: :on_social_step? [/ruby]

Back to Blog