user1486510
user1486510

Reputation: 139

How to pass simple_form hidden_field paramer to create action

I am having trouble passing a parameter defined in a hidden_field of a simple_form to a create action. In our app, a User has many bets through memberships. Membership columns include bet_id, user_id, accepted:boolean, and against:boolean. The against column of Membership indicates whether the user is betting against or betting with the bet. In the view for a particular bet, a user who does not have a membership can choose "Agree" or "Against", which will create a new membership with the appropriate accepted:boolean. Here are the forms found the the bet show page.

    <%= simple_form_for([@bet, @bet.memberships.build]) do |f| %>
      <%= f.hidden_field :against, against: false %>
      <%= f.button :submit, "Agree!" %>
    <% end %>

    <%= simple_form_for([@bet, @bet.memberships.build]) do |f| %>
      <%= f.hidden_field :against, against: true %>
      <%= f.button :submit, "Against!" %>
    <% end %>

Here is the attempted create action

def create
  @membership = current_user.memberships.build(bet_id: params[:bet_id], against: params[:against], accepted: :false)
  redirect_to :back
end

This currently just leaves the accepted value as nil. Any help would really be appreciated! Thanks

Upvotes: 2

Views: 4144

Answers (1)

Bob Roberts
Bob Roberts

Reputation: 1021

Try using this:

<%= f.hidden_field :against, value: false %>

Upvotes: 4

Related Questions