Rails 3: Linking to Image in New Window - ruby-on-rails-3

I want to display text that links to a image that opens a new window (_target), but I don't know the Rails 3 way of doing that. All of my searches have just yielded making the image the link (ie link to(image_tag....)), but that's not what I want to do.
What I'm looking to do is display the text link "Click here to see an example" to the image "example.gif" in my assets/images folder.
I tried:
<%= link_to "Click here to see an example", image_tag("example.gif"), :target => "_blank" %>
but that didn't work. Thanks!

Like this :
<%= link_to "Click here to see an example", image_path("example.gif"), :target => "_blank" %>

Related

User click on image to download public file in rails

I'd like a user of my rails app to be able to click on a link 'download' and that they will then download a png file I have placed in my public folder. ('tool.png')
At the moment I have the incorrect...
<%= link_to "download", '/tool.png', :action => 'download' %>
I have created a download action in the controller:
def download
send_file '/tool.png', :type=>"application/png", :x_sendfile=>true
end
What is happening here is that when a user clicks on the 'download' link it opens tool.png in its own window, rather than actually downloading the file.
Can anyone help me with this?
Thanks
HTML 5
For HTML5 it's actually very simple. You don't need a special controller action.
<%= link_to "download", '/tool.png', :download => 'filename' %>
# 'filename' specifies the new name for the downloaded file
Note: check the docs to see what browsers are supported
< HTML 5
If you want to support all browsers you must use the download action which you setup. The only thing missing is setting up the correct route.
routes.rb
get '/download_pdf', "controller_name#download", :as => :download_pdf
then link your HTML to the correct path, which will call the download action and send the file.
<%= link_to "download", :download_pdf_path
What you need is
<%= link_to "download", '/download', :action => 'download' %>
not
<%= link_to "download", '/tool.png', :action => 'download' %>
Where "/download" is the rails route which need to be specified in routing file.
since in your case your are not actually hitting the controller, you are just accessing http://host/tool.png. Check your development logs for more info, you will see no logs since request is not directly served by rails but with other case you will see them.

How to add html link for a simple_form field in rails 3.2?

Here is the quote_task field in simple form.
<%= f.input :quote_task, :label => t('Quote Task'), :input_html => {:value => #quote_task.id}, :readonly => true %>
Now we want to add an embeded html link for the #quote_task.id to show the content of the quote_task. When a user click the id, he will be directed to the content of the quote task. Something like:
<%= f.input :quote_task, :label => t('Quote Task'), :input_html => {:value => (link_to #quote_task.id.to_s, quote_task_path(#quote_task.id))}, :readonly => true %>
Is there a way to do this in simple_form? Thanks for help.
your expectation for HTML are way beyond any possible semantic.
http://www.w3schools.com/tags/tag_input.asp
http://www.w3.org/html/wg/drafts/html/master/forms.html#the-input-element
Yes it is possible that when one click on a input, it will show desired content. However without JS this wont be possible
input:
<%= f.input :quote_task, :label => t('Quote Task'), :readonly => true, class="redirecter", :'data-quote-task-path'=>quote_task_path(#quote_task.id) %>
coffee script using jQuery:
#app/assets/javascript/my_input.coffee
jQuery ->
$('input.redirecter').click ->
path = $(this).data('quote-task-path')
document.write(path); # ugly redirect, use Ajax
simple solution, but better would be if you load some content from server to your page with Ajax http://api.jquery.com/jQuery.ajax/
but my opinion is that you shouldn't do this at all. Inputs are for forms, forms are for submitting data. What you should be really using is pure link_to without any input due to HTML semantics. If you want it to look like input that you can style it to look like input, point is don't rape input tag for what it not meant to do.
it's not possible to embed anchors within input fields.
you can use javascript to do whatever magic that field should have.
if you want to find out more about javascript. go to amazon, buy a book, read it.

How to handle link_to_function in rails3

This is my code
<%= link_to_function ("Add another url", :id=> "add_another_url_link_id") do |page|
page.insert_html :bottom ,:add_another_url, :partial =>'add_another_url', :locals => {:object =>Url.new, :url => section}
end%>
Here i am showing add_another_url partial under the bottom of add_another_ur div.
I have a list. At the end of row i am showing this link. When i click the link that will show form under the row. But when i click the link that is showing the form under the first row only.
I want to show the form for corresponding row. (I don't know how to use 'this' in this place)
Can Anyone help me.
I assume you generate this link for every row in your list. This is a bad idea because it will generate the same element id (add_another_url_link_id) for every row which is not valid html.
You should either generate individual ids:
<%- #items.each do |item| %>
<%= link_to_function "Add another url", :id => "add_url_for_item_#{item.id}" do |page| %>
...
And use the specific id to find the relevant row.

what are the paperclip rails3 file_field {options}

I installed paperclip and use it to handle photo attachment.
I have a simple form where i can upload photo's works perfect by the way.. But i want to adjust the "browse" button, for example change it's name... Or even better hide the text field, so that there's only an upload button with the text, "upload photo..."
This the form code:
<div class="photoupload"
<%= f.label :description %>
<%= f.text_field :description %>
<%= f.file_field :photo %>
</div>
Regards!
Seems like you can't really change the button, but you can make it invisible, set its z-index (so it is underneath everything else) and use css to position some styled block on top of it.
http://www.quirksmode.org/dom/inputfile.html

Cucumber click only working when #javascript is used?

I know there's a simple fix for this but for the life of me I can't remember what it is.
My feature file is as following:
Scenario: Editing locations
When I edit "Western Australia"
And fill in "Name" with "Tasmania"
And I press "Save"
Then I should see a form success message
And I've defined the 'Edit' step as the following:
When /^I edit "([^"]*)"$/ do |name|
within(:xpath, "//tr[./td[contains(text(), '#{name}')]]") do
find(:css, "a img[alt=Edit]").click
end
end
The HTML for the index page it works on is as follows:
<tr>
<td>Western Australia</td>
<td>WA</td>
<td>
<img alt="Edit" src="/images/icons/pencil.png" title="Edit" />
</td>
</tr>
And then the form HTML:
<%= semantic_form_for [:admin, #location] do |f| %>
<%= f.inputs do %>
<%= f.input :name %>
<%= f.input :abbreviation %>
<% end %>
<%= f.submit "Save" %></li>
<% end %>
As it is, it doesn't work - I get the following error:
And fill in "Name" with "Tasmania" # features/step_definitions/web_steps.rb:39
cannot fill in, no text field, text area or password field with id, name, or label 'Name' found (Capybara::ElementNotFound)
But the form element 'name' is clearly there on the page.
If I add 'Then show me the page' before the 'Fill in' then capybara saves the index page, leading me to think it isn't getting to the edit form at all.
... Yet if I add the '#javascript' tag to the feature, it works perfectly, even though there is no Javascript on this page.
I've solved this once before, but for the life of me I can't work out how...
Well I managed to solve the problem - the issue was with my CSS selector that was clicking the 'Edit' link. I don't know why it didn't work as-was, but I changed the find(:css, "a img[alt=Edit]").click to read click_link('Edit') and it worked perfectly.
Source: http://groups.google.com/group/ruby-capybara/browse_thread/thread/9c997395306d40e2/
For starters, you need to actually "visit" the edit page using the Capybara visit method within your When block. Also, I don't believe you want to use fill_in for inserting text into your tags (at least according to the error message it is only for text fields/areas).