Having some problems getting my params to pass. Any help would be super appreciated..

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…

in new invitations try changing

<%= 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

This is completely wrong. This usually looks something like

params.require(:invitation).permit(:attended_event_id, :attendee_id)

which says that the params hash should contain an element for the key :invitation, and that the two named parameters are allowed to be present. (A more elaborate syntax allows for array and hash values - lookup the docs for ‘strong parameters’)

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 %>

When you use the _tag helpers (as opposed to g.hidden_field, f.check_box), then your parameters aren’t nested with invitation hash as mentioned above. You’ll also make your life much easier if the input names correspond to the model attribute names (i.e. attended_event_id instead of event_selected). It also seems a bit odd (although probably harmless) that you are repeating the event_selected input.

Fred

I noticed a discrepancy in the field name:

def new
@event_selected = Event.find(params[:event_select])

Parameters:

 {"utf8"=>"✓",
 "authenticity_token"=>"GMMg9DwnTRAw4qP/ICqgACUB4d42Pl9Y7hrrNQzO38K8inbgyM00H2etrepjrT35hwIenHfwQPQW08V6QnHl1A==",
 "event_selected"=>"14",

In the find query, you’re using :event_select, and in the posted parameters, its :event_selected.

As Fred pointed out, your use of strong parameters and the form helpers seems wrong, but aside from those problems, it looks like you’re not actually calling your “invite_params” method anyways. If you want to make use of strong parameters, in “new”, you would use, Event.find(invite_params[:event_selected]).

Best,

Brent

So much great help!!

So I have made some changes in response to everyone’s comments as well as some other research and got a simplified version of just passing the event and a manually entered user id into the form and got it to create the invitation object.

My current problem is getting the array of selected user_ids to work and create multiple invitation objects from the form.

I have updated the new.html.erb:

Invite users to <%= @event_selected.name %>

<%= bootstrap_form_for Invitation.new do |f| %>



    <%= f.hidden_field :attended_event_id, :value => @event_selected.id %>
    <% @users.each do |user| %>

  • <%= check_box_tag ‘attendee_ids[]’, user.id %>
    <%= h user.name %>

  • <% end %>



<%= f.submit “Invite Selected Users” %>
<% end %>

``

I had cleaned up the params and tried to get the array iteration to work:

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

``

I’m getting an undefined method `map’ for nil:NilClass error currently. I think I’m getting closer though.

Current hash I’m passing:

Parameters:

 {"utf8"=>"✓",
"authenticity_token"=>"ZvFBC0sqw5zEV0hwRBCu+ri2IeJgVbAx3CMmi7Osac7DyiKQf9Q20Yz7Db3eSE0Vgd9b/0r8XDmEE4P6XnTQNg==",
"invitation"=>{"attended_event_id"=>"3"},
"attendee_ids"=>["3"],

“commit”=>“Invite Selected Users”}

I changed the hidden field to f.hidden_field but kept getting errors if I tried to change the checkbox as well. In reading some of the answers for the error it kept using the check_box_tag to get it to work. Good call out on repeating my event selected. Doesn’t hurt but doesn’t make sense either!

Thanks. In looking at passing the array it seems that I need the ‘[]’, I think when I started and didn’t have them I was getting errors when I would submit the form.

Thanks for taking a look! The “event_select” was what I was passing from the event view. The event_selected was how I was using that in the invitation new form. I was trying to seperate the names so if something broke I knew where it was bringing up the error. I have found that sometimes the naming of objects in rails seems redundant in the examples and it’s hard to figure out what exactly is calling what.