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 }