Rails: hidden fields that don't pass values

I am trying to create a form for a site admin to associate events with a list of previous speakers. The administrator goes to the dynamics, clicks on the "Add to event" button, and a form appears asking which event will be present in the dynamics. The speaker is assumed to be passed as a hidden field, and the events are enumerated through a collection selection. Unfortunately the loudspeaker id doesn't go to the database when the form is submitted.

Why is my form not saving values ​​from the hidden field and how can I fix this? The values ​​from the collection selection pass.

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

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

  <p><%= @speaker.first_name %></p>
  <div class="actions">
    <%= @speaker.id %>
    <%= f.hidden_field(:speaker) %>
    <%= hidden_field_tag('speaker_id',@speaker.id)  %>
  </div>
  <div class="field">
    <%= f.label :event %><br>
    <%= f.collection_select(:event_id, @upcoming_events, :id, :name)%>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

      

And the controller:

def new
    @event_speaker = EventSpeaker.new
    @speaker = Speaker.find(params[:speaker_id])
    @event_speaker.speaker_id = @speaker
    @Time = Time.now
    @upcoming_events = Event.all
  end

  # GET /event_speakers/1/edit
  def edit
  end

  # POST /event_speakers
  # POST /event_speakers.json
  def create
    @event_speaker = EventSpeaker.new(event_speaker_params)

    respond_to do |format|
      if @event_speaker.save
        format.html { redirect_to @event_speaker, notice: 'Event speaker was successfully created.' }
        format.json { render action: 'show', status: :created, location: @event_speaker }
      else
        format.html { render action: 'new' }
        format.json { render json: @event_speaker.errors, status: :unprocessable_entity }
      end
    end
  end

      

AND...

 def event_speaker_params
      params.require(:event_speaker).permit(:speaker_id,:event_id) 
    end

      

+3


source to share


1 answer


You must do

<%= f.hidden_field :speaker_id, :value => @speaker.id %>

      

This will create your speaker_id nested inside event_speaker so your form looks like this:



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

  <p><%= @speaker.first_name %></p>
  <div class="actions">
    <%= f.hidden_field :speaker_id, :value => @speaker.id %>
  </div>
  <div class="field">
    <%= f.label :event %><br>
    <%= f.collection_select(:event_id, @upcoming_events, :id, :name)%>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

      

More about check hidden field

in rails

+5


source







All Articles