# Default value depending on the controller action

**URL:** <https://www.ruby-forum.com/t/default-value-depending-on-the-controller-action/194443>\
**Category:** Rails\
**Created:** [September 6, 2010, 4:39pm UTC](https://www.ruby-forum.com/t/default-value-depending-on-the-controller-action/194443 "2010-09-06T16:39:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vitaly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/838e76/32.png) [@vitaly](https://www.ruby-forum.com/u/vitaly)\
**Post date:** [September 6, 2010, 4:39pm UTC](https://www.ruby-forum.com/t/default-value-depending-on-the-controller-action/194443/1 "2010-09-06T16:39:26Z")

</div>

The solution that is good for me:

\<% if controller.action\_name == ‘new’ %\>  
\<%= f.text\_field :image\_url, :value =\> ‘no image’ %\>  
\<% else %\>  
\<%= f.text\_field :image\_url %\>  
\<% end %\>

and then in index.erb set smth like that  
\<%= product.image\_url == ‘no image’ ? image\_tag("/images/  
empty.png", :alt =\> ‘Book cover’, :class =\> ‘list\_image’) :  
image\_tag("/images/#{product.image\_url}", :alt =\> ‘Book cover’, :class  
=\> ‘list\_image’) %\>  
to set work on the default value for this.

---

<div class="post-metadata">

**Author:** ![vitaly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/838e76/32.png) [@vitaly](https://www.ruby-forum.com/u/vitaly)\
**Post date:** [September 6, 2010, 4:39pm UTC](https://www.ruby-forum.com/t/default-value-depending-on-the-controller-action/194443/2 "2010-09-06T16:39:51Z")

</div>

You’re not really asking a question, but if you’re looking for  
feedback on your code, I’m happy to provide my 2 cents.

It appears that image\_url just returns a string as a path to an image  
under /images/ on your web server.

I would override the default accessor for image\_url in your model code  
eg.

def image\_url  
url\_string = read\_attribute(:image\_url)  
url\_string.blank? ? “defaultpath.png” : url\_string  
end

This way you can check the state of the model to determine what to  
render in the view instead of testing which controller action is being  
called. For example, what if you had a product without a url ? Your  
code would break when you call the edit action even though you would  
want to render the default value for product. This could could be  
taken a lot further, but I’ll wait to see if this is helpful to you.

Also, image\_tag references images relative to /images/ on your web  
server, so the image you’d be looking for would be under the page /  
images/images/#{image\_url}

Hope that helps,

Luke

---

<div class="post-metadata">

**Author:** ![vitaly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/838e76/32.png) [@vitaly](https://www.ruby-forum.com/u/vitaly)\
**Post date:** [September 6, 2010, 4:44pm UTC](https://www.ruby-forum.com/t/default-value-depending-on-the-controller-action/194443/3 "2010-09-06T16:44:06Z")

</div>

Oops, I’m sorry. I thought I answered to some open topic with this,  
and didn’t know I started a new one.

Well, thank you for you idea, Luke. I though about putting work with  
data to model, and I think you are right.  
As for empty field, I add validates to model to check that this field  
isn’t empty, and also add explanation to the error note.  
Sure, I can allow to leave it just blank for ‘no image’, but validate  
for presence will make user sure he didn’t forget to put the image  
url, and along with format validation with regular expression, it will  
minimize the number of mistakes I think.

Well, actually that was a test and I’m just learning Rails, so I guess  
my way is not really good on the point of usability, and it’s not a  
right thing, but i’m just testing 🙂

Peace,  
Vit
