如何为rails 3.2中的simple_form字段添加html链接? [英] How to add html link for a simple_form field in rails 3.2?

查看:162
本文介绍了如何为rails 3.2中的simple_form字段添加html链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是简单形式的quote_task字段。

 <%= f.input:quote_task,:label => t('Quote Task'),:input_html => {:value => @ quote_task.id},:readonly => true%> 

现在我们要为@ quote_task.id添加一个嵌入式html链接来显示quote_task。当用户点击该ID时,他将被引导至报价任务的内容。例如:

 <%= 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%> 

有没有办法在simple_form中做到这一点?感谢您的帮助。

解决方案

您对HTML的期望超越了任何可能的语义。

http://www.w3schools.com/tags/tag_input.asp
http:// www.w3.org/html/wg/drafts/html/master/forms.html#the-input-element



是的,有可能当点击一个输入,它会显示所需的内容。然而,如果没有JS,这是不可能的



输入:

 < %= f.input:quote_task,:label => t('Quote Task'),:readonly => true,class =redirecter,:'data-quote-task-path'=> quote_task_path(@ quote_task.id)%> 

使用jQuery的咖啡脚本:

 #app / assets / javascript / my_input.coffee 
jQuery - >
$('input.redirecter')。click - >
path = $(this).data('quote-task-path')
document.write(path); #丑陋的重定向,使用Ajax

简单的解决方案,但是如果您将某些内容从服务器加载到您的带有Ajax的页面 http://api.jquery.com/jQuery.ajax/

但我认为你不应该这样做。输入用于表单,表单用于提交数据。由于HTML语义,你应该真正使用的是纯粹的 link_to ,没有任何输入。如果你希望它看起来像输入,你可以将它看起来像输入样式,那么point不要强奸 input 标记来表示它不应该做的事情。


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.

这篇关于如何为rails 3.2中的simple_form字段添加html链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆