简体   繁体   中英

Create landing page using Rails

I'm trying to create a landing page for an app I'm creating. Any suggestions on how I go about this. Nothing complex just one page with a form that users can add their email address and it goes into a database. I am not sure if I should use Devise Gem for this? I feel like there is a simpler way. Much appreciated. I'm a super noob. Just completed onemonthrails.

Devise is an authentication library. If you are looking to have users sign up to access different parts of your app (requiring authentication), then you could use devise.

If you just want to gather data from people visiting your app, you can just create a simple resource.

# generate model
bundle exec rails g model UserSignup email

# run migration
bundle exec rake db:migrate

Then configure your routes.

# config/routes.rb
resources :user_signups, only: [:new, :create]
root 'user_signups#new'

Then your controller:

# app/controllers/user_signups_controller.rb
class UserSignupsController < ApplicationController

  def new
    @user_signup = UserSignup.new
  end

  def create
    @user_signup = UserSignup.new(user_signup_params)

    if @user_signup.save
      redirect_to root_url, notice: "Thank you for expressing interest."
    else
      render action: 'new', alert: "Signup failed."
    end
  end

  private
    def user_signup_params
      params.require(:user_signup).permit(:email)
    end

end

And finally your view:

# app/views/user_signups/new.html.erb

<h1>Signup to Express Your Interest!</h1>

<%= form_for @user_signup do |f| %>
  <% if @user_signup.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@user_signup.errors.count, "error") %> prohibited your registration from being completed:</h2>

      <ul>
      <% @user_signup.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :email %><br>
    <%= f.text_field :email %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

That should get you started and you can start fleshing out the pages, email validations, etc. Also, you may want to redirect to somewhere other than the root_url from the create action, but you can probably figure out how to do that.

So that will get the data in the database, but if you want to view the collected data through the app (rather that directly in the database), that's when you'd use something like devise to lock down the places in your app that display the user data.

You should check out Landing page in rails by Daniel Kehoe of RailsApp. It's a great starting point and he even has a tutorial (it's paid though but cheap)

Devise is more for user authentication, you don't need it if you're just collecting email addresses.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM