I think I got it - it was a URI issue.
When a name with a space was created, the user was routed to 
/user_groups/The A Team.

It my routes I have /user_groups/:group_name routing to that group's home 
page.
However, since those spaces don't register correctly, the app seems to run 
another POST request to create a group instead.

I simply changed my redirect to redirect_to 
'/user_groups/'[email protected]_name.gsub(/\s/,'%20'), :notice => "Your group 
has been created" 
Let me know if you think there's any problems with the way I handled this.

Thanks,
Daniel

On Wednesday, September 10, 2014 1:00:13 PM UTC-7, Daniel Bogart wrote:
>
> Kevin,
>
> When I submit the form to create a group my app creates the group, saves 
> it to the user, then tries to create the group *again* (which it 
> shouldn't), sees that there is already a group with that group name 
> (obviously, because it was just created seconds ago), and throws the error.
>
> Your refactoring of my controller method helped - now the group is 
> created, assigned to the user, user gets redirected to the 
> '/user_groups/'[email protected]_name appropriately, but the error message: 
> "Error: 
> group name may already be taken. Search, or try a new name." still gets 
> posted. 
>
> I just can't figure out why the create method is being run more than 
> once...
>
> -Daniel
>
> On Wednesday, September 10, 2014 12:24:03 PM UTC-7, Kevin Thompson wrote:
>>
>> Daniel,
>>
>> Is it creating multiple groups with the same name in your database? What 
>> it looks like could be happening is that your group name may already exist. 
>> If that’s the case, the `create_user_group` method call fails due to your 
>> uniqueness validation (see Ben’s note about `strip`), then the user’s 
>> user_group is assigned to a record that has not been persisted. Ultimately 
>> the `valid?` check on @group fails and redirects to your page that includes 
>> your error message.
>>
>> The simplest change you could make to account for this is to check the 
>> validity of your group before saving the user record:
>>
>> def create
>>   @group = current_user.create_user_group(group_params)
>>   if @group.valid?
>>     current_user.user_group = @group
>>     current_user.save
>>     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
>>
>>
>> Also, if your reasoning for removing the spaces from the group name is to 
>> use the name as a URL parameter, you may want to consider using gsub in 
>> addition to strip in order to substitute spaces for another character:
>>
>> def strip_blanks
>>   self.group_name = self.group_name.strip.gsub(/\s/,'-')end
>>
>>
>> That gsub statement will replace any space character inside of the name 
>> with a dash.
>>
>>
>> On Wednesday, September 10, 2014 11:26:21 AM UTC-7, Daniel Bogart wrote:
>>>
>>> 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