ruby-on-rails-3formssimple-form

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 embedded HTML link for the @quote_task.id to show the content of the quote_task. When a user click the id, they 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?


Solution

  • 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.