user938363
user938363

Reputation: 10350

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.

Upvotes: 0

Views: 886

Answers (2)

equivalent8
equivalent8

Reputation: 14227

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.

Upvotes: 1

phoet
phoet

Reputation: 18835

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.

Upvotes: 0

Related Questions