N
N
Nikita Filatov2016-03-06 19:11:10
Ruby on Rails
Nikita Filatov, 2016-03-06 19:11:10

How to add a new field to a Ruby On Rails model?

In general, there is a model. Created a migration like rails g migration AddEmploymentToGuests. A field was added to the database added an input to guests#new to enter this field here is the guests#new page itself

<%= form_for(@guest) do |f| %>
  <% if @guest.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@guest.errors.count, "error") %> prohibited this guest from being saved:</h2>

      <ul>
      <% @guest.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <input placeholder= "Ваше имя" type="text", name= "guest[first_name]", id="guest_first_name" required>
  </div>
  <div class="field">
      <input placeholder = "Ваша фамилия" type="text", name = "guest[second_name]", id = "guest_second_name" required>
  </div>
  <div class="field">
      <input placeholder = "Email" type="email" name = "guest[email]", id ="guest_email" required>
  </div>
  <div class="field">
      <input placeholder = "Номер телефона" type="text" required name="guest[phone_number]" id ="guest_phone_number">
  </div>
  <%= f.text_field :employment %>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

Then I enter the data in the fields, I press the button, but the data from employment is not sent. This field was definitely added to the model, here is a clipping from debug:
raw_attributes:
  id: 16
  first_name: asd
  second_name: asda
  email: [email protected]
  phone_number: '8123'
  created_at: '2016-03-06 15:54:37.993403'
  updated_at: '2016-03-06 15:54:37.993403'
  employment:

but for some reason the data is not sent there

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Korolev, 2016-03-06
@M0NSTERC4T

Most likely it is necessary to add to the allowed parameters (permit params) in the controller.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question