Add new field to the bank with Rails

Asked

Viewed 218 times

2

I inserted a new column in my table contents and also includes the field in the form with the same column name in the creation table (client_id), but this data is not entered when I create a new record.

I’ve already added the symbol in the controller;

params.require(:content).permit(:title, :content, :client_alteration, :target, :scheduled_to, :status, :comment, :client_id)

THE HTML

<div class="field">
<%= f.label 'Cliente' %>
<%= select_tag 'client_id', options_from_collection_for_select(@clients, 'id', 'name') %>

  • Rafael, did you mean that Rails does not auto-increment your id ?

2 answers

1


@Rafael, the problem is that you are not passing client_id as a Contents attribute (using f ) in your form, in cases such as yours, I particularly like to use the collection_select, would look something like this:

<div class="field">
  <%= f.label 'Cliente' %>
  <%= f.collection_select :client_id, @clients, :id, :name %>
</div>

0

The select_tag creates a specific field within params and not within the content. You need the field client_id inside content (params[:content][:client_id]), but the field being passed is probably params[:client_id].

  • I managed the controller with the scaffold and I thought he’d already include this inside the param, where I would need to change then?}

  • I’m not sure it’s a good idea to use mass assignment with foreign key fields. Usually in these cases I urge the entity and associate it with the object being saved. For example: client = Client.find(params[:client_id] @content.client = client

Browser other questions tagged

You are not signed in. Login or sign up in order to post.