How to add html link for a simple_form field in ra

2019-09-02 06:01发布

问题:

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.

回答1:

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.



回答2:

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.