LoginSignup
4
5

More than 5 years have passed since last update.

RailsのルートページにDeviseの登録フォームを埋め込んだように見せる方法

Last updated at Posted at 2013-08-31

RailsのTopページに、Twitterみたいに登録フォームを埋め込めたらなぁと思ったのですが、
Deviseのビューをルートに設定できないらしいので、少しズル?をしてルートに設定(してるように)しました。

1.Deviseの登録画面をトップ画面用に編集する

/app/view/users/registrations/new.html.erb
 <!-- /.navbar -->
<div class="container">
# 略
</div>

<div class="container">
<!-- Registration Form -->
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>

<div><%= f.label :email %><br />
<%= f.email_field :email, :autofocus => true %></div>

<div><%= f.label :password %><br />
<%= f.password_field :password %></div>

<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>

<div><%= f.submit "Sign up" %></div>
<% end %>
<%= render "users/shared/links" %>
</div>

今回は、ユーザーモデルに認証機能をつけたので、usersというフォルダに入っている登録画面の
ERBを編集します。

2.トップ画面のコントローラーをルートに設定する

static_pagesコントローラのhomeメゾッドでトップ画面を表示していたので、
static_pages#homeをルートに設定します。

/config/routes.rb
root :to => "static_pages#home" #追加

3.トップページのコントローラーにリダイレクトを設定する

/app/controller/static_pages_controller.rb
class StaticPagesController < ApplicationController
    def home
        redirect_to '/users/sign_up'
    end
end

んで、サーバー立ち上げて確認すると、ちゃんとルートから自動で登録ページに移動してます!!

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5