I am trying to create a form for a site administrator to associate events with a list of previous speakers. The administrator navigates to the speaker, clicks on a button to "Add to event" and a form comes up asking for which event the speaker is attending. The speaker is supposed to be passed in as a hidden field, and the events are listed via a collection select. Unfortunately, the speaker id is not passing into the database when I submit the form.
Why is my form is not saving values from the hidden field, and how do I about fixing this? Values from the collection select are passing.
<%= 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
You should do
<%= f.hidden_field :speaker_id, :value => @speaker.id %>
This will generate your speaker_id nested inside event_speaker so your form will look 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 %>
For details checkout hidden field
in rails
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With