I have a User Group table with a group_name value that is entered when the 
group is created. If there is a space in the group name, for example "The A 
Team", the group name is still created, but it is not assigned to the 
current user, and it throws the error message in my if/else statement in 
the controller. Code is as follows:

Controller:

def create
    @group = current_user.create_user_group(group_params)
    current_user.user_group = @group
    current_user.save
    if @group.valid?
        redirect_to '/user_groups/'[email protected]_name, :notice => "Your group 
has been created"
    else
        redirect_to '/user_groups/', :error => "Error: group name may already 
be taken. Search, or try a new name."
    endend

Model

class UserGroup < ActiveRecord::Base

has_many :users

has_secure_password
validates :password, :presence => true

validates :group_name, :presence => true, :uniqueness => true

before_validation :strip_blanks
def strip_blanks
  self.group_name = self.group_name.stripend

Create form:

<div class="form-group">
    <label for="Group Name">Enter group name</label>
    <%= f.input :group_name, :required => true, :autofocus => true, :maxlength 
=> 40, :input_html => { :class => "form-control" }, :label => false, 
:placeholder => "Group name" %></div><div class="form-group">
    <label for="Password">Password</label>
    <%= f.input :password, :required => true, :autofocus => true, :maxlength => 
40, :input_html => { :class => "form-control" }, :label => false, :placeholder 
=> "Group password" %></div>
    <%= f.button :submit, :class => "btn btn-md btn-warning" %></div>


Link to StackOverflow post if you want some 
points:http://stackoverflow.com/questions/25757686/rails-spaces-in-string-used-for-name-causes-validation-error
Thanks!

Daniel

-- 
-- 
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby
--- 
You received this message because you are subscribed to the Google Groups "SD 
Ruby" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to