Reputation: 65
I have an extremelly basic rails 4 application. It's like a landing page with a video embeded from youtube and with a field for the user put his email and zipcode...
So, I am having trouble to save this user's email and zip code, actually, I am new at rails and I don't know how to do it... I have created a model called Information, with an email and zipcode(both strings)... I have a view called home with this code:
<%= form_for :information do |f| %>
<div class="form-group">
<%= f.label :Email_address %>
<%= f.text_field :email, class: "form-control", :autofocus => true, placeholder: "Enter email" %>
</div>
<div class="form-group">
<%= f.label :Zip_Code %>
<%= f.text_field :zipcode, class: "form-control", :autofocus => true, placeholder: "Zip Code" %>
</div>
<div class="form-group">
<%= f.submit "Submit", class: "btn btn-danger" %>
</div>
<% end %>
But when I click submit nothing happens, I think I should create a controller but I dont know what to put on it to make it work! What should I do to collect just this two information in the best way? Thanks very much!
Upvotes: 0
Views: 1298
Reputation: 76784
Let me explain some things for you, as you're new
Objects
Ruby (& Rails by virtue of being built on Ruby) is object orientated. This means that every interaction you make with the Rails backend (the landing page doesn't interact with the backend initially), has to be centred around objects
Although you've done well by creating the respective objects (with the Information
model), you need to appreciate the process of creating, populating & initializing the respecting objects
--
Form
You're using form_for
requires an ActiveRecord object. This is your major downfall - say your landing page is at application#welcome
, here's the
#app/controllers/application_controller.rb
Class ApplicationController < ActionController::Base
def welcome
@information = Information.new #-> creates "information" object
end
end
The form_for
method can then take the @information
object, to populate the data as required:
#app/views/application/welcome.html.erb
<%= form_for @information do |f| %>
<%= f.text_field :email %>
<%= f.text_field :zipcode %>
<%= f.submit %>
<% end %>
Notice how you're using the @information
object here? This is where the ActiveRecord object comes in - allowing you to "populate" that as you wish
--
Backend
#config/routes.rb
root: "application#welcome"
resources :information, only: :create
The form_for
will send your request to information_controller.rb
:
#app/controllers/information_controller.rb
Class InformationController < ApplicationController
def create
@information = Information.new(information_params)
if @information.save
flash[:notice] = "Message Sent - Thank you!"
redirect_to "application#welcome"
end
end
private
def information_params
params.require(:information).permit(:email, :zipcode)
end
end
This will be able to take the @information
object, populate it in the database, and then redirect to the original "landing" page again.
Upvotes: 1
Reputation: 52357
You will need controller with two actions: :new
and :create
.
in console rails g controller informations
(I assume your model called Information
).
In this file
def new
@information = Information.new
end
def create
@information = Information.new(information_params)
redirect_to @information
end
private
def information_params
params.require(:information).permit(:email, :zipcode)
end
And then this code should go into /view/informations/new
as new.erb
<%= form_for @information do |f| %>
<div class="form-group">
<%= f.label :Email_address %>
<%= f.text_field :email, class: "form-control", :autofocus => true, placeholder: "Enter email" %>
</div>
<div class="form-group">
<%= f.label :Zip_Code %>
<%= f.text_field :zipcode, class: "form-control", :autofocus => true, placeholder: "Zip Code" %>
</div>
<div class="form-group">
<%= f.submit "Submit", class: "btn btn-danger" %>
</div>
<% end %>
And take a look at some quick tutorial to have a basic understanding of how MVC established. This guide http://www.railstutorial.org/book is what almost everyone start.
Upvotes: 2