Ruby on Rails Thursday, October 15, 2015

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: 


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


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"} 


On Wednesday, October 14, 2015 at 12:36:39 PM UTC-5, Brent wrote:
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


On Tuesday, October 13, 2015 at 2:46:28 PM UTC-6, Prkl8r wrote:

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




--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/423c2e1a-5c49-4cf5-bc6d-2415de25d387%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment