On Dec 15, 2010, at 6:30 PM, Aston J. wrote:
> Hi Philip, I tried your code:
>
> def create
> name = params[:topic][:name].titleize
> @topic = Topic.find_or_create_by_name(name)
> current_user.topics << @topic
> if @topic.save
> redirect_to @topic, :notice => 'Topic was successfully created.'
> else
> render :action => "new"
> end
> end
>
> But now I get this if a topic has already been created:
>
> 'Validation failed: User has already been taken'
That's not the code I sent... why are you trying to save a @topic that you
either a) already found or b) just created (and saved) ??
If you are concerned the topic might not get created right then...
def create
name = params[:topic][:name].titleize
@topic = Topic.find_or_create_by_name(name)
if @topic
current_user.topics << @topic
redirect_to @topic, :notice => 'Topic was successfully created.'
else
render :action => "new"
end
end
>
>> What is visible? If visible is related to the user's ability to see
>> that topic then it shouldn't be set in the topic itself.
>> You'll need an intermediate model (look up has many through) to handle
>> that.
>
> Visible will be used like a soft-delete, that only admin users will be
> able to modify. By default all topics are visible, but if a topic is
> against our rules :visible will be changed to false. That way it will
> not show (to site visitors) and it will also catch any future violations
> (as it's already been created).
Ok. Then you'll need to set that up as well in the above code after you've
found the topic.
Hey... not sure why I thought of this before, but why is your create() method
*finding* a topic at all? You might want to see if there's a better way to
organize your actions...
>
>> Take a look at the titleize() method and get rid of hashtag entirely...
>
> Titleize only capitalizes the first letter of each word... I want to
> remove the space too, so it because a #hastag :-)
Ah yes. Missed that...
name.titleize.gsub(/\s+/, '')
-philip
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.