In my Rails 3 web app, I have a Twitter text field in one of the forms and on the user page it displays it.
<%= f.label :twitter, "Twitter Username" %>
<%= f.text_field :twitter, :value => "@" %>
And on the user page:
<% if @user.twitter? %>
<div class="twitter"><%= @user.twitter %></div>
<% end %>
The problem is, when a user doesn’t enter their Twitter username into the field, it keeps @ in the field and displays it on the user page. Because of this I would like to make it so if it just has @ then it doesn’t display it.
Looks like the
@value gets persisted into the database if the user doesn’t specify a twitter name, right? Are you sure you want that?You could, in the controller, when saving the user, do something like:
This will ensure that the
User#twitterfield only gets set if something is specified.But to answer your question specifically, you can do something like this in the view: