我在Rails 2.3.3上,我需要创建一个发送帖子请求的链接.
我有一个看起来像这样的:
= link_to('Resend Email',
{:controller => 'account', :action => 'resend_confirm_email'},
{:method => :post} )
Run Code Online (Sandbox Code Playgroud)
这会在链接上产生适当的JavaScript行为:
<a href="/account/resend_confirm_email"
onclick="var f = document.createElement('form');
f.style.display = 'none';
this.parentNode.appendChild(f);
f.method = 'POST';
f.action = this.href;
var s = document.createElement('input');
s.setAttribute('type', 'hidden');
s.setAttribute('name', 'authenticity_token');
s.setAttribute('value', 'EL9GYgLL6kdT/eIAzBritmB2OVZEXGRytPv3lcCdGhs=');
f.appendChild(s);
f.submit();
return false;">Resend Email</a>'
Run Code Online (Sandbox Code Playgroud)
我的控制器操作正在运行,并设置为不渲染:
respond_to do |format|
format.all { render :nothing => true, :status => 200 }
end
Run Code Online (Sandbox Code Playgroud)
但是当我点击链接时,我的浏览器会下载一个名为"resend_confirm_email"的空文本文件.
是什么赋予了?
我有一个链接,我需要提交一个帖子请求.通常,我会使用jQuery并阻止链接的默认行为,然后将表单提交到目标.这似乎是Rails应该能够帮助我的东西.果然,该link_to
方法有一个指定POST http方法的选项:
link_to "Profile", 'http://example.com/profile', method: :post
Run Code Online (Sandbox Code Playgroud)
这有效,但我也需要添加2个参数.我试过了:
link_to "Profile", 'http://example.com/profile', method: post, param1: 'value1', param2: 'value2'
Run Code Online (Sandbox Code Playgroud)
这只是将这些参数添加到<a>
HTML元素中,但是在单击链接时没有提交这些参数:
<a rel="nofollow" param1="value1" param2="value2" data-method="post" href="http://example.com/profile">Profile</a>
Run Code Online (Sandbox Code Playgroud)
有没有办法使用参数link_to
或任何其他Rails方法执行POST请求?我正在使用Rails 3.2.9.
post ruby-on-rails link-to ruby-on-rails-3 ruby-on-rails-3.2
在使用link_to方法生成的链接主体中获取嵌入式HTML的最佳方法是什么?
我基本上想要以下内容:
<a href="##">This is a <strong>link</strong></a>
Run Code Online (Sandbox Code Playgroud)
我一直试图按照Rails和<span>标签的建议来解决这个问题,但没有运气.我的代码如下所示:
def picture_filter
#...Some other code up here
text = "Show items with " + content_tag(:strong, 'pictures')
link_to text, {:pics => true}, :class => 'highlight'
end
Run Code Online (Sandbox Code Playgroud)
#...
<%=raw picture_filter %>
#...
Run Code Online (Sandbox Code Playgroud) 我想让访问者可以选择下载一些pdf.我试过了:
<%= link_to "abc", "/data/abc.pdf"%>
<%= link_to "abc", "/data/abc.pdf", :format => 'pdf' %>
Run Code Online (Sandbox Code Playgroud)
和一些变化但它们似乎不起作用.我一直在No route matches [GET] "/data/abc.pdf"
我将pdf文件放在名为data的文件夹中,该文件夹位于assets文件夹中.任何帮助,将不胜感激.
我想在Ruby上添加link_to函数的确认消息.
= link_to 'Reset message', :action=>'reset' ,:confirm=>'Are you sure?'
Run Code Online (Sandbox Code Playgroud)
任何想法为什么它不起作用?
ruby link-to ruby-on-rails-3 ruby-on-rails-3.1 ruby-on-rails-3.2
这似乎是一个菜鸟问题,但简单的答案是在逃避我.我需要调用link_to
一个ActionController
方法来吐出一个HTML链接. ActionView::Helpers::UrlHelper.link_to
调用url_for
,但这会调用AV模块的版本而不是控制器的版本.我设法强迫这样做,通过推杆做我想要的
#FIXME there must be a better way to mixin link_to
alias_method :self_url_for, :url_for
include ActionView::Helpers::UrlHelper
alias_method :url_for, :self_url_for
Run Code Online (Sandbox Code Playgroud)
在控制器中.但是,我仍然不确定它为什么会起作用.有人可以解释方法范围并隐藏这里发生的事情吗?什么是更好的混合方式link_to
(或者通常只包括模块中的一些方法),所以我可以在控制器中调用它(生成带有链接的flash字符串就是用例.)
请不要讲MVC - 如果有的话,link_to
应该在一个单独的模块中url_for
.从噪音的数量来看,很多人遇到这个看似微不足道的障碍,并最终浪费了一个小时做"Rails方式",真正想要的是一分钟黑客让我的应用程序现在工作.是否有"Rails方式"可能与助手一起做这件事?还是更好的红宝石方式?
我知道这可能是一个非常简单的概念.我正在尝试创建一个控制器和操作的链接.例如,我的布局文件中有一个链接,用于在单击链接时更新记录,因此我需要能够链接到控制器和操作.我怎么做到这一点?
我有以下内容:
<%= link_to my_path, method: :delete, confirm: 'Delete?', class: 'link-delete', 'data-message' => 'Are you sure?', 'data-severity' => 'danger', :remote => true do %>
<i class="icon-trash"></i>
<% end %>
Run Code Online (Sandbox Code Playgroud)
它会启动一个Bootstrap Modal进行确认,我想挂钩ajax调用,以便我可以显示一个微调器或某种文本.
我知道我可以使用不引人注目的javascript来监听点击事件,如果我不在我的link_to中使用':remote => true'
jQuery ->
$('.link-delete').live 'click', (event) ->
$('.link-delete').html("Loading...") #THE MSG OR ANIMATION I WANT TO DISPLAY
$.get(this.href, null, null, 'script')
false
Run Code Online (Sandbox Code Playgroud)
但是在使用':remote => true'时不确定如何将两者结合起来
有什么建议?
谢谢您的帮助
我正在开发一个允许用户创建帐户的网站.创建用户时的一个属性是用户个人网站.当我尝试使用这样的用户网站时:
<%= link_to @user.site, @user.url %>
Run Code Online (Sandbox Code Playgroud)
生成的url是: http://0.0.0.0:3000/www.userswebsite.com
我认为这是因为link_to的@user部分......但我怎样才能将其链接到www.userwebsite.com?
我已经看过如何添加<span>
标签但是我没有看到<span>
使用Rails 3 放置我想要的位置的示例link_to
:
<a href="#" class="button white"><span id="span">My span </span>My data</a>
Run Code Online (Sandbox Code Playgroud)
我尝试过类似的东西:
<%= link_to(content_tag{:span => "My span ", :id => "span"} @user.profile.my_data, "#", {:class => "button white"}) %>
Run Code Online (Sandbox Code Playgroud)
但那没用.
link-to ×10
hyperlink ×2
ruby ×2
action ×1
actionview ×1
ajax ×1
controller ×1
download ×1
external ×1
helpers ×1
html ×1
javascript ×1
mixins ×1
post ×1
rest ×1