1

I'm rails noob. I have no idea where is problem. I cant save messages to database by help contact form. But I can save it by help "ContactForm.new" in rails console.

My contact_form_controller.rb is:

class ContactFormController < ApplicationController
  def new
  end

  def create
    @contact_form = ContactForm.create!(params[:contact_form])
    @contact_form.save!
    redirect_to root_path
  end

  def show
    @contact_form = ContactForm.all
  end
end

My contact_form.html.erb is:

<div class="all-banners-width">
    <figure class="green-background">
        <%= form_for :contact_form do |f| %>
            <p>
                <%= f.label :name %><br>
                <%= f.text_field :name %>
            </p>

            <p>
                <%= f.label :phone %><br>
                <%= f.text_field :phone %>
            </p>

            <p>
                <%= f.label :email %><br>
                <%= f.text_field :email %>
            </p>

            <p>
                <%= f.label :text %><br>
                <%= f.text_field :text %>
            </p>

            <p>
                <%= f.submit %>
            </p>
        <% end %>
    </figure>
</div>

My DB migration file is:

class CreateContactForms < ActiveRecord::Migration
  def change
    create_table :contact_forms do |t|
      t.string :name
      t.string :phone
      t.string :email
      t.string :text

      t.timestamps null: false
    end
  end
end

my routes.rb is:

Rails.application.routes.draw do
  resources :users
  resources :sessions, only: [:new, :create, :destroy]
  resources :contact_forms, only: [:new, :create, :destroy]
  root 'static_pages#home'
  match '/',        to: 'static_pages#home',       via: 'post'
  match '/manager', to: 'static_pages#manager',    via: 'get'
  match '/manager', to: 'sessions#create',         via: 'post'
  match '/signin',  to: 'sessions#new',            via: 'get'
  match '/signout', to: 'sessions#destroy',        via: 'delete'

There are not any errors, when I send a message via this form. Thanks

verrom
  • 429
  • 4
  • 18
  • Can you please edit your question to include the whole of the form? You've missed the top. Also, please give more details about what actually happens when you try to save, including any errors which occur. – Max Williams Sep 29 '15 at 07:56
  • Which Rails version you are using? – Pavan Sep 29 '15 at 08:01
  • what do you mean can't save? does the :create action called after the `Save` button is pressed? `ContactForm.new` doesn't save the record to DB – Малъ Скрылевъ Sep 29 '15 at 08:01
  • Because you are using `save` (rather than `save!`) it will fail silently when there's an error. Instead of `save` + `new`, try `ContactForm.create!(params[:contact_form])`. It should tell you what the problem is when trying to save. – Drenmi Sep 29 '15 at 08:04
  • I thied ContactForm.create!(params[:contact_form]) and use save! instead save. There is not any new record in DB and no one error. I've update code list in the question. I use rails 4.2.4 – verrom Sep 29 '15 at 08:30

2 Answers2

3

As you mentioned you are using Rails 4.2.4, you should use strong parameters in the controller

Your create method would look like the below

def create
  @contact_form = ContactForm.new(contact_form_params)
  if @contact_form.save
     redirect_to root_path, notice: "Successfully Created"
  else
     render :new
  end
end

And define contact_form_params method like below

private
def contact_form_params
  params.require(:contact_form).permit(:name, :phone, :email, :text)
end

Also worth changing the new method to below

def new
  @contact_form = ContactForm.new
end

and use it in the form

<%= form_for @contact_form do |f| %>
Amit Badheka
  • 2,447
  • 4
  • 15
  • 27
Pavan
  • 32,201
  • 7
  • 42
  • 69
  • I've update my code. When I use `` instead `` so I have an error: `ArgumentError in StaticPages#home`, `First argument in form cannot contain nil or be empty` – verrom Sep 29 '15 at 08:42
  • @verrom You should consider changing your file name from `contact_form.html.erb` to `new.html.erb` or define `contact_form` method and put `@contact_form = ContactForm.new` in it – Pavan Sep 29 '15 at 08:44
  • I've defined `contac_form` method like this: `def contact_form @contact_form = ContactForm.new end` And there is the same error. – verrom Sep 29 '15 at 08:57
  • Can it be because `StaticPageController` has method with similar name: `def manager @contact_forms = ContactForm.all end`? I use it for displaying all contact messages. – verrom Sep 29 '15 at 09:37
  • @verrom If my answer helped you, please accept it by ticking it green :) – Pavan Sep 29 '15 at 14:20
1

I'm rails noob.

Here's what I'd use:

#config/routes.rb
resources :messages

#app/models/message.rb
class Message < ActiveRecord::Base
end

#app/controllers/messages_controller.rb
class MessagesController < ApplicationController
    def new
       @message = Message.new
    end
    def create
       @message = Message.new message_params
       @message.save
    end

    private

    def message_params
       params.require(:message).permit(:name, :phone, :email, :text)
    end
end

#app/views/messages/new.html.erb
<%= form_for @message do |f| %>
   <%= f.text_field :name %>
   <%= f.text_field :phone %>
   <%= f.text_field :email %>
   <%= f.text_field :text %>
   <%= f.submit %>
<% end %>

class CreateMessages < ActiveRecord::Migration
  def change
    create_table :messages do |t|
      t.string :name
      t.string :phone
      t.string :email
      t.string :text

      t.timestamps null: false
    end
  end
end

Notes

  1. Messages

Why would I call it messages?

Think about what you're saving in your DB. It's not the form, it's the message. Maybe we could call it something else, but I wouldn't call it contact_form.

  1. create!

Firstly, don't use the bang operator (!) unless you want to manipulate that particular object in its present form.

Although this isn't the case for this particular scenario, it's generally the case that if you want to manipulate your variable without redeclaring it, you'd invoke the "bang" variant of the method:

Calling create! basically goes against every convention you'd expect to find in Rails. You should stick with @object.save

Community
  • 1
  • 1
Richard Peck
  • 73,250
  • 8
  • 84
  • 139
  • thanks a lot. I see. yes, I'll rename the Controller. All other errors I've fixed according to your recomendations. But now I have an error: `No route matches [GET] "/contact_form"`. Could you give an advice how to fix it? – verrom Sep 29 '15 at 11:27