I am trying to pass parameters from an "Events" controller, to an "Invitations" controller and from the Invitations#new to the Invitations#create views. I think I'm pretty close to getting this wrapped up bit keep getting: "param is missing or the value is empty: " errors when I run it.
In order to pass the event.id from the Event#show view I am doing the following through the "Invite Guests" link.
events/show.html.erb
<% @user.owned_events.each do |e| %>
<ul>
<li><%= e.name %> | <%= link_to "Invite Guests", invitations_new_path(:event_select => e.id) %></li>
That should pass the current event selected as event_select.
I am then using that event id as well as all of the user ids(minus the current_user) to create a list of possible invitees.
invitations_controller.rb:
class InvitationsController < ApplicationController
helper_method :current_user
def new
@event_selected = Event.find(params[:event_select])
@users = User.where("id != ?", current_user.id )
end
def create
@invitation = Invitation.new(invite_params)
end
private
def invite_params
params.require(:attended_event_id => params[:event_selected], :attendee_id => params[:user_ids].first )
end
end
My view showing the list of users and after selecting a checkbox, should pass the event_selected and user_ids.
invitations/new.html.erb
<h3>Invite users to <%= @event_selected.name %></h3>
<%= bootstrap_form_for Invitation.new do |f| %>
<br>
<ul>
<% @users.each do |user| %>
<li>
<%= hidden_field_tag :event_selected, @event_selected.id %>
<%= check_box_tag 'user_ids[]', user.id %>
<%= h user.name %>
</li>
<% end %>
</ul>
<br>
<%= submit_tag "Invite Selected Users" %>
<% end %>
I am trying to get this to work to select just a single user at a time before moving to create multiple objects from the selected event combined with all the results in the user_id array. When I select a single user I keep getting the missing param error but looking at the hash, it seems like everything is there.
param is missing or the value is empty: {:attended_event_id=>"14", :attendee_id=>"3"}
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"GMMg9DwnTRAw4qP/ICqgACUB4d42Pl9Y7hrrNQzO38K8inbgyM00H2etrepjrT35hwIenHfwQPQW08V6QnHl1A==",
"event_selected"=>"14",
"user_ids"=>["3"],
"commit"=>"Invite Selected Users"}
Pretty new at this coding thing but this is the first problem I haven't been able to solve through a lot stackoverflow searches. I seem to be missing something here and I'm sure it's just a simple thing. Then again I could be completely missing the mark trying to go about this without following a bit more guided path...
<%= check_box_tag 'user_ids[]', user.id %>
to
<%= check_box_tag 'user_ids', user.id %>
def invite_params params.require(:attended_event_id => params[:event_selected], :attendee_id => params[:user_ids].first ) end
My view showing the list of users and after selecting a checkbox, should pass the event_selected and user_ids.
invitations/new.html.erb
<h3>Invite users to <%= @event_selected.name %></h3> <%= bootstrap_form_for Invitation.new do |f| %> <br> <ul> <% @users.each do |user| %> <li> <%= hidden_field_tag :event_selected, @event_selected.id %> <%= check_box_tag 'user_ids[]', user.id %> <%= h user.name %>
def new
@event_selected = Event.find(params[:event_select])
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"GMMg9DwnTRAw4qP/ICqgACUB4d42Pl9Y7hrrNQzO38K8inbgyM00H2etrepjrT35hwIenHfwQPQW08V6QnHl1A==",
"event_selected"=>"14",
<h3>Invite users to <%= @event_selected.name %></h3>
<%= bootstrap_form_for Invitation.new do |f| %>
<br>
<ul>
<%= f.hidden_field :attended_event_id, :value => @event_selected.id %>
<% @users.each do |user| %>
<li>
<%= check_box_tag 'attendee_ids[]', user.id %>
<%= h user.name %>
</li>
<% end %>
</ul>
<br>
<%= f.submit "Invite Selected Users" %>
<% end %>
class InvitationsController < ApplicationController
helper_method :current_user
def new
@event_selected = Event.find(params[:attended_event_id])
@users = User.where("id != ?", current_user.id )
end
def
create
@invitations= invite_params[:attendee_ide_ids].map do |attendee_id|
Invitation.new(
attended_event_id: invite_params[:attendent_event_id],
attendee_id: attendee_id
)
end
if @invitations.any?(&:invalid?)
flash.now[:error] = "Failure!"
redirect_to root_path
else
@invitations.each(&:save!)
flash.now[:success] = "Invited!"
redirect_to root_path
end
end
private
def invite_params
params.require(:invitation).permit(:attended_event_id, attendee_id: [])
end
end
Parameters:
{"utf8"=>"✓", "authenticity_token"=>"ZvFBC0sqw5zEV0hwRBCu+ri2IeJgVbAx3CMmi7Osac7DyiKQf9Q20Yz7Db3eSE0Vgd9b/0r8XDmEE4P6XnTQNg==", "invitation"=>{"attended_event_id"=>"3"}, "attendee_ids"=>["3"],