1

An organization has many members, and a relationship has been created between both models. A member of an organization is allowed to create additional members for that specific organization. This works fine. A system admin can go to the profile of any organization and then click 'create new member' to create a new member that organization. However, currently for the admin the organization_id does not save with the newly created member, thereby causing errors. Does anyone have an idea why the organization_id does not save?

Members controller:

  def new
    if current_user.admin?
      if params[:organization_id].nil?
        flash[:danger] = "Please select an organization first"
        redirect_to organizations_path
      else
        @organization = Organization.find(params[:organization_id])
        @member = @organization.members.build
      end
    else
      @member = current_organization.members.build
    end
  end

  def create
    if current_user.admin?
      @member = Member.new(new_params)
    else
      @member = current_organization.members.build(new_params)
    end
    if @member.save
      @member.send_activation_email
      flash[:success] = "A confirmation email will be sent to the new user."
      redirect_to member_url(@member)
    else                            
      render 'new'
    end
  end

private
def new_params
  params.require(:member).permit(:email,
                                  :username,
                                  :password,
                                  :password_confirmation)
end

Members new view:

<%= form_for(@member) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>

  <% if current_user.admin? %>
    <%= f.text_field :organization_id, placeholder: 'Organization id', autocomplete: 'off', class: 'form-control', disabled: "disabled" %>
  <% end %>

  <%= f.email_field :email, placeholder: 'Email', autocomplete: 'off', class: 'form-control' %>
  <%= f.text_field :username, maxlength: 15, placeholder: 'Username', autocomplete: 'off', class: 'form-control' %>
  <%= f.password_field :password, placeholder: 'Password', autocomplete: 'off', class: 'form-control' %>
  <%= f.password_field :password_confirmation, placeholder: 'Confirm password', autocomplete: 'off', class: 'form-control' %>

  <%= f.submit "Sign up", class: "formbutton btn btn-default" %>
<% end %>

The organization profile view contains the following link to the member new view:

<%= link_to image_tag("add.png", title: "add member", height: '15'), new_member_path(organization_id: @organization.id) %>

Part of the members model:

  belongs_to :organization
  default_scope -> { includes(:organization).order('organizations.org_name , username') }
  attr_accessor :remember_token, :activation_token, :reset_token
  before_save   :downcase_email, :downcase_username
  before_create :create_activation_digest
  #validates :organization_id, presence: true  # Commented this out because of a joined form that doesn't work with this validation.
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
  validates :email,           presence: true,
                              length: { maximum: 255 },
                              format: { with: VALID_EMAIL_REGEX },
                              uniqueness: { case_sensitive: false }
  VALID_USERNAME_REGEX = /\A[a-zA-Z0-9_-]+\z/i
  validates :username,        presence: true,
                              length: { in: 6..15 },
                              format: { with: VALID_USERNAME_REGEX },
                              uniqueness: { case_sensitive: false }
Nick
  • 3,496
  • 7
  • 42
  • 96

2 Answers2

0

In your def new_params, you are not permitting :organization_id.

If this is for security reasons, you can update your create action as follows:

def create
    if current_user.admin?
      @member = Member.new(new_params)
      @member.organization_id = params[:member][:organization_id]
    else
     ....
end

Also in your form, if you disable a field, it won't be sent as a params. You should add a hidden_field for :organization_id.

<% if current_user.admin? %>
    <%= f.text_field :organization_id, placeholder: 'Organization id', autocomplete: 'off', class: 'form-control', disabled: "disabled" %>
    <%= f.hidden_field :organization_id, value: @organization.id %>

<% end %>
AbM
  • 7,326
  • 2
  • 25
  • 28
  • I added organization_id to new_params but still with the same result. It does not save the organization_id. Could perhaps `def create` in the controller be defined incorrectly for the admin? – Nick May 05 '15 at 16:44
  • what error are you getting? `@member.errors.full_messages` – AbM May 05 '15 at 16:48
  • I'm getting errors because the profile of the new member loads variables from the organization, such as @member.organization.avatar. Also, looking inside the delopment database, confirms the new record/member has `nil` in the organization_id column. – Nick May 05 '15 at 16:50
  • So a record is created? Can you show your `Member` model? Also why the is params `orguser` not `member`? Where is this defined? – AbM May 05 '15 at 16:56
  • I'll add it the the orinal post. Orguser is a typo. The record is indeed created. – Nick May 05 '15 at 16:57
0

The problem turned out to be disabled: "disabled" in the form. A disabled field does not get send on submit. Changing it to readonly: "readonly" solved it. See: What's the difference between disabled="disabled" and readonly="readonly" for HTML form input fields?

Community
  • 1
  • 1
Nick
  • 3,496
  • 7
  • 42
  • 96