vendredi 16 septembre 2016

Mail_form gem showing no errors and not accepting validations

i'm using the mail_form gem and it works, as in it sends the email and refreshes the page. But it shows no errors and i can also leave fields blank and use invalid emailadresses. Also i can not use my @contact variable from my contact action inside the contact view. Instead of a usual contacts_controller.rb i put the codes for the new and create action inside my oqba_controller.rb like this:

  def contact
   @contact = Contact.new
  end

  def create
   @contact  = Contact.new(params[:contact])
   @contact.request = request
   respond_to do |format|
    if @contact.deliver
     format.html { redirect_to :contact, notice: 'Uw bericht is verzonden, wij nemen zo spoedig mogelijk contact met u op!' }
     format.json { render :create, status: :created, location: @contact }
    else
     format.html { render :contact }
     format.json { render json: @contact.errors, status: :unprocessable_entity }
    end
   end
  end

In my model contact.rb:

class Contact < MailForm::Base
  attribute :email,      :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
  attribute :subject,    :validate => true
  attribute :message,    :validate => true
  attribute :nickname,   :captcha  => true


  def headers
    {
        :subject => "Contactform",
        :to => "xx@gmail.com, xx@gmail.com",
        :from => %("#{subject}"  <#{email}>)
    }
  end
end

In my contact.html.erb view

    <%= form_for :contact do |f| %>
        <form class="form-login">
          <div class="form-group">
            <%= f.label :email %><br />
            <%= f.email_field :email, class: 'center-block form-control', autofocus: true %>
          </div>
          <div class="form-group">
            <%= f.label :subject %><br/>
            <%= f.text_field :subject, class: 'center-block form-control' %>
          </div>
          <div class="form-group">
            <%= f.label :message %><br/>
            <%= f.text_area :message, class: 'center-block form-control' %>
          </div>
          <div class="form-group">
            <%= f.hidden_field :nickname, hint: 'leave this field blank' %>
          </div>
          <div class="form-actions">
            <%= f.submit "Send message", class: 'btn btn-red center-block' %>
          </div><br/>
        </form>
    <% end %>

Note: when i use <%= form_for @contact do |f| %> it says:

undefined method `contacts_path'

In my routes.rb i have:

  resources :oqba, only: [:contact, :create]
  match 'contact'    =>  'oqba#contact',  via: [:get, :post]

Something is going wrong or i missed a big step but i can't grasp what it is. Any help would be much appreciated!

Aucun commentaire:

Enregistrer un commentaire