Sunday, September 5, 2010

[Rails] Re: Default value depending on the controller action

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
On Sep 5, 8:08 am, Vitaly <iva.softw...@gmail.com> wrote:
> 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.

--
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 rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate