Kathan
Kathan

Reputation: 1458

Model validations causing error Rails 4

I have a form for people to donate. It requires email and a name. I have not yet added stripe.. just trying to get the form to work first and save their email/name to the db.

When I add a presence validation to my model, it won't accept anything. It just returns the f.error notification. When I get rid of validations, the form submits successfully but nothing is saved to the data base. The rails console simply returns nil for name and email.

Still new to rails, so probably something simple. Any recommendations would be awesome.

donation.rb (model):

class Donation < ActiveRecord::Base
  attr_accessor :name, :email

  validates :name,
  presence: true

  validates :email,
  presence: true
end

donations_controller:

class DonationsController < ApplicationController
  def new
    @donation = Donation.new
  end

  def create
    @donation = Donation.new(params[donation_params])
    if @donation.valid?
      redirect_to root_path, notice: "Thank you. We have received your donation."
    else
      flash[:alert] = "An error occurred, please try again."
      render :new
    end
  end

private

  def donation_params
    params.require(:donation).permit(:name, :email)
  end
end

routes.rb:

get 'donations', to: 'donations#new', as: 'donations'
post 'donations', to: 'donations#create'

new.html.erb (donation view):

<body class ="CU">
  <div class="authform">
    <%= simple_form_for @donation, :url => donations_path do |f| %>
    <h1 style = "text-align:center; color:black;">Donate</h1>
    <%= f.error_notification %>
    <div class="form-group">
      <%= f.text_field :name, placeholder: 'Name', :autofocus => true, class: 'form-control' %>
    </div>
    <div class="form-group">
      <%= f.text_field :email, placeholder: 'Email', class: 'form-control' %>
    </div>
    <%= f.submit 'Donate', :class => 'btn btn-lg btn-danger btn-block' %>
    <% end %>
  </div>
</body>

Let me know if you need anything else. Thank you guys.

Upvotes: 0

Views: 52

Answers (2)

Andrey Deineko
Andrey Deineko

Reputation: 52357

This part

@donation = Donation.new(params[donation_params])

should actually be:

@donation = Donation.new(donation_params)

Also your create method doesn't save the record. Use save instead of valid?

Also why did you put breakline in your validation definitions?

Also, use RESTful routes, that is:

resources :donations

instead of manually defining each route.

Also, since you use strong params, you don't need attr_accessor (which should have been attr_accessible it case of rails, but whatever). You would use attr_accessor for defining virtual attributes, but I am sure it was not the case, since name and email are database backed fields.

And, finally, just walk through Rails guides - you'll find all needed info.

Upvotes: 3

jon snow
jon snow

Reputation: 3072

Because you are not saving it, try this if @donation.save instead of if @donation.valid? in create

Upvotes: 0

Related Questions