This allows users to upload an file from the browser into my Rails app using the paperclip gem. Once the file is uploaded it gets saved into the filesystem. When the user then goes in and the show method or the edit method is evoked the image is shown to the user. This is fine for image files but for .csv and .txt files I don't want to show the preview in the browser. This code is clunky and I know there is a better way to do this.<% if @user.image? %><%=filetype = @user.image.url %><br/><%if filetype.include? .jpeg %>    <b>isJpeg</b>    <%= image_tag @user.image.url %> <br />    <%= link_to @user.image.url, @user.image.url  %><% end %> <%if filetype.include? .gif %>    <b>isGif</b>    <%= image_tag @user.image.url %> <br />    <%= link_to @user.image.url, @user.image.url  %><% end %><%if filetype.include? .png %>    <b>isPNG</b>    <%= image_tag @user.image.url %> <br />    <%= link_to @user.image.url, @user.image.url  %><% end %><%if filetype.include? .jpg %>    <b>isJPG</b>    <%= image_tag @user.image.url %> <br />    <%= link_to @user.image.url, @user.image.url  %><% end %><%if filetype.include? .csv %>    <b>isCSV</b>    <p>Your file was a csv file and has no preview</p>    <%= link_to @user.image.url, @user.image.url  %><% end %><%= image_tag @user.image.url %> <br /><%= link_to @user.image.url, @user.image.url  %><% end %>
Uploading a file from the browser
beginner;ruby;ruby on rails;image;file system
tokland is right (on both counts), you should push all that logic into a helper. You can also add a bit of OpenStruct into the mix to make the helper nicer:# in app/helpers/application_helper.rb or another helperdef user_image_info(user)    info = OpenStruct.new(:has_image? => false)    return info if(!user.image?)    # There might be better ways to do this but I don't know paperclip.    u = user.image.url    %w[jpeg gif png jpg csv].find do |ext|        # A small abuse of `find` but reasonable in this case.        info.is = is#{ext.upcase} if(u.include?(.#{ext}))    end    if(info.is == 'isCSV')        info.preview_link = '<p>Your file was a csv file and has no preview</p>'.html_safe    else        info.preview_link = (image_tag(user.image.url) + '<br>').html_safe    end    infoendThen in your ERB, you could do something like this:<% info = user_image_info(@user) %><% if info.has_image? %>    <b><%= info.is %></b>    <%= info.preview_link %>    <%= link_to @user.image.url, @user.image.url  %><% end %>