Stanmx
Stanmx

Reputation: 455

How save input from static page to database in rails

i want to save a input "email" from static pages to the database. Right now, i have a emails model with a :address field, but i can't save the value when click the submit input.

Layout with form

# layout/website.html.erb
<div>
  <%= form_for @email, :url => newsletter_path, :method => :post do |email| %>
    <%= email.text_field :address %>
    <%= email.submit "Go" %>
  <% end %>
</div>

Pages Controller

# controllers/pages_controller.rb
def home
  newsletter
end

def newsletter
  @email = Email.new(params[:address])

  if @email.valid?
    redirect_to(request.referer, :notice => "The suscription has been sent successfully.")
  else
    redirect_to(request.referer, :alert => "Please add an valid email address.")
  end
end

Routes

# routes.rb from static pages
  get "/home" => 'pages#home', :as => :home
  get "/pricing" => 'pages#pricing', :as => :pricing
  get "/logo-gallery" => 'pages#logo_gallery', :as => :logo_gallery
  get "/blog" => 'pages#blog', :as => :blog
  get "/blog/:id" => 'pages#post', :as => :post
  get "/contact" => 'pages#contact', :as => :contact
  match '/contact' => 'pages#create', :as => :contact, :via => :post
  get "/faqs-and-terms" => 'pages#faqs_and_terms', :as => :faqs_and_terms

  match "/newsletter" => 'pages#newsletter', :as => :newsletter, :via => :post

Model

# models/email.rb 
class Email < ActiveRecord::Base
  attr_accessible :address
  validates :address, :format => { :with => %r{.+@.+\..+} }, :allow_blank => true
end

With this code, when visit /home right now i'm received:

ActionController::ActionControllerError in PagesController#home
Cannot redirect to nil!

I hope you can help me, what can i do to fix this and get a better code. Thank you.

Upvotes: 2

Views: 1247

Answers (2)

Aman Garg
Aman Garg

Reputation: 4218

In newsletter action email record is only initialized but not saved. And you have no need to call newsletter method inside home action every time. Just update your code like this.

Pages Controller

# controllers/pages_controller.rb
.....
# no need to call newsletter action here
def home      
end

def newsletter
  @email = Email.new(params[:email])

  if @email.save
    redirect_to((request.referer || '/'), :notice => "The suscription has been sent successfully.")
  else
    redirect_to((request.referer || '/'), :alert => "Please add an valid email address.")
  end
end

Layout with form

As @email is not defined in controller, so you should initialize an Email object in form.

# layout/website.html.erb
<div>
  <%= form_for Email.new, :url => newsletter_path, :method => :post do |email| %>
    <%= email.text_field :address %>
    <%= email.submit "Go" %>
  <% end %>
</div>

I hope that it should work.

Upvotes: 1

hedgesky
hedgesky

Reputation: 3311

You should use Email.create(params[:email]) instead of Email.new(params[:address]) to persist records in DB. Also, in your view you code add hidden_field

<%= email.text_field :address %>
<%= hidden_field_tag :referer, :value => request.path %>
<%= email.submit "Go" %>

And then in controller:

redirect_to params[:referer]

UPD Also, please notice that you should use params[:email] in your controller, not params[:address]. You can invoke raise params.inspect in your controller to inspect params sent by form.

Upvotes: 3

Related Questions