Hidden attribute not populating field in form for Rails 3.2

Matteo

I'm implementing an invitation system and I want the new user form to pre-populate the user's email address in the email address field on the form (eventually, I will refactor this so it's not a form_field), so that the user doesn't have to type in all their information, just enter a password.

I have created the getter/setter methods in the users.rb model like this:

  def invitation_token
    invitation.invitation_token if invitation
  end
  def invitation_token=(invitation_token)
    self.invitation = Invitation.find_by_invitation_token(invitation_token)
  end

INVITATION MODEL

class Invitation < ActiveRecord::Base

  #--== ASSOCIATIONS
  belongs_to :sender, :class_name => 'User'
  has_one :recipient, :class_name => 'User'
  #--== CALLBACKS
  before_create :generate_token
  before_create :recipient_is_not_registered
  before_create :decrement_sender_count, :if => :sender
  #--== VALIDATIONS
  validates_presence_of :recipient_email
  #validate :recipient_is_not_registered
  validate :sender_has_invitations, :if => :sender
  #--== METHODS
  private
    def recipient_is_not_registered
      if User.find_by_email(recipient_email)
        false
      else
        true
      end
    end

    def sender_has_invitations
      unless sender.invitation_limit > 0
        redirect_to root_url
      end
    end

    def generate_token  #TODO: MOVE to lib/generate_token.rb
      self.invitation_token = Digest::SHA1.hexdigest([Time.now, rand].join)
    end

    def decrement_sender_count
      sender.decrement! :invitation_limit
    end

end

USER CONTROLLER

class UsersController < ApplicationController
  def new
    @user = User.new(:invitation_token => params[:invitation_token])
    @user.email = @user.invitation.recipient_email if @user.invitation
  end

  def create
    @user = User.new(user_params)
    if @user.save
      session[:user_id] = @user.id
      redirect_to root_url, notice: "Thank you for signing up!"
    else
      render "new"
    end
  end

 ...

  def user_params
    params.require(:user).permit(:email, :password, :password_confirmation, :admin)
  end
end

views/users/_form.html.erb

<%= form_for @user do |f| %>  

  <%= f.hidden_field :invitation_token %>

  <div class="field">
    <%= f.label :email %><br />
    <%= f.text_field :email %>
  </div>
  <div class="field">
    <%= f.label :password %><br />
    <%= f.password_field :password %>
  </div>
  <div class="field">
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation %>
  </div>
  <div class="field">
    <%= f.check_box :admin %>
    <%= f.label :admin %>
  </div>
  <div class="actions"><%= f.submit %></div>
<% end %>

I was following Ryan Bates' RC#124 - Beta Invitations, and got stuck here. His code doesn't produce the error, so I should mention that this is a Rails 3.2.18 app.

When I reload the form, the user's email isn't populated in the form. The relevant log shows:

Started GET "/signup.914823d28d07b747213ec3de47f89ad537169e34" for 127.0.0.1 
at 2016-04-30 20:24:47 -0600
Processing by UsersController#new as 
  User Load (1.0ms)  SELECT "users".* FROM "users" WHERE "users"."auth_token" = 'rOHiKmDcceytxi_t151YIQ' LIMIT 1
  Invitation Load (0.0ms)  SELECT "invitations".* FROM "invitations" WHERE "invitations"."invitation_token" IS NULL LIMIT 1
  Rendered users/_form.html.erb (5.0ms)
  Rendered users/new.html.erb within layouts/application (6.0ms)
Completed 200 OK in 102.0ms (Views: 25.0ms | ActiveRecord: 3.0ms)

So it appears that the invitation_token isn't being passed in, since the log shows it is NULL.

I have gone over the RC code from top to bottom and can't find out why it's not being passed.

Any help would be appreciated. Thanks.

UPDATE: The output from the view source is:

<input id="user_invitation_token" name="user[invitation_token]" type="hidden" />, so it's not being passed along.

Anthony E

Set the value on the hidden field by passing the value: key:

<%= f.hidden_field :invitation_token, value: some_value %>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Rails form_tag, adding a hidden field

From Dev

Rails form_tag, adding a hidden field

From Dev

Auto Populating the first instance of a nested form field in Ruby on Rails

From Dev

ZF2 Doctrine2 populating a form with a date field

From Dev

Trying to access hidden field in rails 3

From Dev

Re-populating form field

From Dev

Re-populating form field

From Dev

Set a Rails form hidden field from enum model

From Dev

Rails - hidden_field in a partial form - erroring or not rendering

From Dev

Rails for nested array hidden input field in form (Haml)

From Dev

Rails Form, How to set value of hidden field with jquery

From Dev

Hidden field in a Google Form

From Dev

Rails change date format in form field attribute value

From Dev

Rails change date format in form field attribute value

From Dev

Select2 placeholder not working in rails app with hidden_field

From Dev

Select2 placeholder not working in rails app with hidden_field

From Dev

Simple form date_field not populating for edit

From Dev

Simple form date_field not populating for edit

From Dev

Show Hidden Form Field with Jquery

From Dev

Automatically add hidden field to form

From Dev

Show Hidden Form Field with Jquery

From Dev

Retrieve hidden field in form_for

From Dev

Rails: belongs_to association through a form not populating

From Dev

Ruby on Rails, simple_form - I want to set a hidden field of the simple_form from a html select_tag field

From Dev

symfony2: trying to add a type="number" attribute to a form field

From Dev

Symfony2 entity form field and its attribute name

From Dev

symfony2: trying to add a type="number" attribute to a form field

From Dev

send value of form to hidden field of gravity form

From Dev

Title attribute required for a hidden select field

Related Related

  1. 1

    Rails form_tag, adding a hidden field

  2. 2

    Rails form_tag, adding a hidden field

  3. 3

    Auto Populating the first instance of a nested form field in Ruby on Rails

  4. 4

    ZF2 Doctrine2 populating a form with a date field

  5. 5

    Trying to access hidden field in rails 3

  6. 6

    Re-populating form field

  7. 7

    Re-populating form field

  8. 8

    Set a Rails form hidden field from enum model

  9. 9

    Rails - hidden_field in a partial form - erroring or not rendering

  10. 10

    Rails for nested array hidden input field in form (Haml)

  11. 11

    Rails Form, How to set value of hidden field with jquery

  12. 12

    Hidden field in a Google Form

  13. 13

    Rails change date format in form field attribute value

  14. 14

    Rails change date format in form field attribute value

  15. 15

    Select2 placeholder not working in rails app with hidden_field

  16. 16

    Select2 placeholder not working in rails app with hidden_field

  17. 17

    Simple form date_field not populating for edit

  18. 18

    Simple form date_field not populating for edit

  19. 19

    Show Hidden Form Field with Jquery

  20. 20

    Automatically add hidden field to form

  21. 21

    Show Hidden Form Field with Jquery

  22. 22

    Retrieve hidden field in form_for

  23. 23

    Rails: belongs_to association through a form not populating

  24. 24

    Ruby on Rails, simple_form - I want to set a hidden field of the simple_form from a html select_tag field

  25. 25

    symfony2: trying to add a type="number" attribute to a form field

  26. 26

    Symfony2 entity form field and its attribute name

  27. 27

    symfony2: trying to add a type="number" attribute to a form field

  28. 28

    send value of form to hidden field of gravity form

  29. 29

    Title attribute required for a hidden select field

HotTag

Archive