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

北战南征 提交于 2019-12-12 03:43:00

问题


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.



来源:https://stackoverflow.com/questions/19238721/how-to-add-html-link-for-a-simple-form-field-in-rails-3-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!