Acrux
Acrux

Reputation: 406

param is missing or the value is empty error with patch link_to

Is my approach of using a link_to wrong in updating the value of an attribute or is there simply something wrong with my syntax? Any ideas on how I can fix this?

I have an error when I try to update a single attribute using link_to. I have the following code in my views:

<%=link_to "Pay", event_attendee_path(event_attendee, :paid => true), :method => :patch%>

I am using the link_to inside a loop :<% @event_attendees.each do |event_attendee| %><%end%>

I get the error: "param is missing or the value is empty: event_attendee" when I try to run my code.

I have the following in my controller:

def event_attendee_params
  params.require(:event_attendee).permit(:attendee_id, :event_id, :paid)
end

Rake routes:

event_attendee GET    /event_attendees/:id(.:format)      event_attendees#show
                    PATCH  /event_attendees/:id(.:format)      event_attendees#update
                    PUT    /event_attendees/:id(.:format)      event_attendees#update
                    DELETE /event_attendees/:id(.:format)      event_attendees#destroy

Upvotes: 0

Views: 70

Answers (3)

andy
andy

Reputation: 8875

The issue is that when you pass a model to a route as you've done the only thing that is sent to the server is the id of the model. In the server it's params[:id].

In your controller you're requiring that params have a key called :event_atendee but actually the only thing you're sending is the id.

On a patch route you usually need the id to get the object you're going to update, so so far all is. However you probably want to update other values too? So I'd look at documentation on how to send form data in a link_to with a patch method.

One way is to include the keys in the route but there might be a better way.

Upvotes: 1

Michael Yurkovetc
Michael Yurkovetc

Reputation: 156

You can use

@event_attendee.update(paid: params[:paid])

in your controller, or use

form_for(@event_attendee)

on view, if you want use permit_params

Upvotes: 0

Greg
Greg

Reputation: 6650

it should be

params.require(:id).permit(:attendee_id, :event_id, :paid)

You require :event_attendee but routes parse it and sets it as :id

Upvotes: 0

Related Questions