如果我想转到我可以使用的页面上的"whatever_id",我如何使用link_to来正常转到页面上的特定(html)ID
<a href="http://www.example.com/index/mypage#whatever_id>Click Here</a>
Run Code Online (Sandbox Code Playgroud)
但我想使用我的link_to
<%= link_to "click here" , {:action => "mypage", :controller => "index" }, :id => "#whatever_id"%>
Run Code Online (Sandbox Code Playgroud)
辅助方法.有谁知道如何做到这一点?可能吗?
Rails 2.3.4
我试图找出新的link_to,Rails 3但我仍然没有得到它在Rails 2我做:
<%= link_to_remote "My Link",:url=>{:action=>:myaction},:with=>"'data='+$('#someField').attr('value')" %>
Run Code Online (Sandbox Code Playgroud)
但是使用Rails 3中的新语法应该怎么做?
我正在尝试类似的东西
<%=link_to "My Link",{:action=>"myaction"},:with=>"'data='+$('#someField').attr('value');",:remote=>true%>
Run Code Online (Sandbox Code Playgroud)
但是我没有在控制器的动作中获得数量参数
我有一个看起来像这样的观察者:
class CommentObserver < ActiveRecord::Observer
include ActionView::Helpers::UrlHelper
def after_create(comment)
message = "#{link_to comment.user.full_name, user_path(comment.user)} commented on #{link_to 'your photo',photo_path(comment.photo)} of #{comment.photo.location(:min)}"
Notification.create(:user=>comment.photo.user,:message=>message)
end
end
Run Code Online (Sandbox Code Playgroud)
基本上我正在使用它做的是当有人在他们的一张照片上发表评论时为某个用户创建一个简单的通知消息.
这失败并显示错误消息:
NoMethodError (undefined method `link_to' for #<CommentObserver:0x00000102fe9810>):
Run Code Online (Sandbox Code Playgroud)
我原ActionView::Helpers::UrlHelper以为包括会解决这个问题,但似乎没有效果.
那么,我如何在我的观察者中包含URL助手,或者以其他方式呈现这个?我很乐意将"消息视图"移动到局部或类似的东西中,但是观察者没有相关的视图来将其移动到......
ruby-on-rails link-to url-helper observer-pattern ruby-on-rails-3
我正在使用Ruby on Rails 3.0.9,我想生成一个link_to传递一些自定义参数的URL.也就是说,有一个articles_path(www.my_web_site_name.com/articles)我想生成如下内容:
link_to 'Sample link title', ... # Here I should implement the code
# => 'http://www.my_web_site_name.com/articles?param1=value1¶m2=value2&...
Run Code Online (Sandbox Code Playgroud)
link_to为了实现这一目标,我如何编写"aàRubonon Rails Way"语句?如果我想通过传递一些自定义参数来'link_to''article_path(@article)'怎么办?
在某些地方,我已经看到使用类似的东西,link_to 'Sample link title', :param1 => 'value1', :param2 => 'value2'但在这种情况下,它不涉及命名路由(生成的URL指的是当前路径).
我是Rails的新手所以请耐心等待.
我想在我的食堂模型上实现"喜欢",所以我在我的食堂资源中创建了一个自定义路线
resources :canteens do
resources :meals
resources :comments
match "/like", :to => "canteens#like", :as => "like"
end
Run Code Online (Sandbox Code Playgroud)
因此在canteens控制器中创建了这个动作,我只是增加一个计数器
def like
canteen = Canteen.find(params[:canteen_id])
Canteen.increment_counter("likes_count", canteen.id)
redirect_to canteen
end
Run Code Online (Sandbox Code Playgroud)
所以,手动输入URL localhost:3000/canteens/1/like工作得很好,但显然我想创建一个按钮,所以我做了一个
<h2>Likes count</h2>
<p><%= @canteen.likes_count %> likes</p>
<p><%= link_to "Like this canteen", canteen_like_path %></p>
Run Code Online (Sandbox Code Playgroud)
它不起作用.我检查了耙路线,它就在那里(canteen_like).我究竟做错了什么?
我陷入了一个不会那么复杂的问题,但我只是没有把事情做对.
假设我有两个型号:
class Notification < ActiveRecord::Base
belongs_to :device
validates :number, presence: true
end
Run Code Online (Sandbox Code Playgroud)
和
class Device < ActiveRecord::Base
belongs_to :user
has_many :notifications, :dependent => :destroy
//rest omitted for brevity
end
Run Code Online (Sandbox Code Playgroud)
使用嵌套路线如下:
resources :devices do
resources :notifications
end
Run Code Online (Sandbox Code Playgroud)
和这样的通知控制器:
class NotificationsController < ApplicationController
before_filter :authenticate_user!
before_action :set_device, :only => [:index, :new, :create]
before_filter :load_notification, only: :create
load_and_authorize_resource :device
load_and_authorize_resource :notification, :through => :device
def index
end
def new
@notification = @device.notifications.build
end
def create
params.each do |param|
logger.debug param
end
@notification …Run Code Online (Sandbox Code Playgroud) 当我在Rails中使用link_to标记时,我正试图找出在后台播放wav文件的最佳方法(类似HTML5).
这是我的一个观点的示例link_to:
<%= link_to 'At Station', at_station_mdt_index_path, :class => 'btn btn-success btn-medium', :method => :put, :remote => true %>
Run Code Online (Sandbox Code Playgroud)
我想弄清楚当按下按钮时如何使用audio_tag来触发声音.我尝试在link_to ERB中组合audio_tag,但得到各种语法错误.
任何例子都将非常感谢.
更新时间01/04/14-10:18am CT:声音一次正确发射.但是,由于将link添加到link_to链接不再触发rails路径来更改对象,因此只播放声音
查看代码:
<%= link_to 'En Route', en_route_mdt_index_path(:call_id => call.id), :class => 'btn btn-warning btn-medium', :method => :put, :remote => true, :id => "er" %>
<%= link_to 'On Scene', on_scene_mdt_index_path(:call_id => call.id), :id => 'to', :class => 'btn btn-primary btn-medium', :method => :put, :remote => true, :id => "os" %>
<%= link_to 'To Hospital', to_hospital_mdt_index_path(:call_id => call.id), :class …Run Code Online (Sandbox Code Playgroud) 我是Rails的新手,通常link_to为正常的unnested路由设置一个帮助器,如下所示:
link_to "Delete", article_path(article.id), method: :delete, data: {confirm: "Are you sure?"}
Run Code Online (Sandbox Code Playgroud)
但是我正在学习嵌套路由,似乎我需要提供带有两个参数的路径,例如:
link_to "(Delete)", article_comments_path(comment.article_id, comment.id), method: :delete, data:{comfirm: 'Are you sure?'}
Run Code Online (Sandbox Code Playgroud)
然而,这似乎不起作用.我已经看到你可以这样格式化link_to:
link_to 'Destroy Comment', [comment.article, comment], method: :delete, data: { confirm: 'Are you sure?' }
Run Code Online (Sandbox Code Playgroud)
但这似乎让我感到困惑,因为没有定义路径,路径所需的值也没有直接指定.
是否可以格式化嵌套link_to路径,如上面的unnested路径,还是需要格式化,如第三个例子所示?如果是这样,有人可以尝试解释如何将此数组格式转换为Rails执行的URL吗?
谢谢
路线:
article_comment_path - DELETE - /articles/:article_id/comments/:id(.:format) - 评论#stroy
我需要添加一个链接来从assets/docs /Физика.pdf下载文件我不知道该怎么做.我想在这里这样做:在视图中 -
<%= link_to "download", '/??????.pdf', :download => 'filename' %>
Run Code Online (Sandbox Code Playgroud)
我收到一条错误消息:
No route matches [GET] "/%D0%A4%D0%B8%D0%B7%D0%B8%D0%BA%D0%B0.pdf"
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?请帮帮我
我有一个链接,我需要传递一系列参数,其中2个参数始终是nill.
link_to go_to_action_path(id, par_01, par_02, par_03, par_04)
Run Code Online (Sandbox Code Playgroud)
在某些情况下是par_01和par_02 nil在其他par_03和par_04.
如果我在变量中有这些值:
par_01 = nil
par_02 = nil
par_03 = 'a'
par_04 = 'b'
Run Code Online (Sandbox Code Playgroud)
然后在go_to_action行动中:
p01 = params[:par_01]
p02 = params[:par_02]
p03 = params[:par_03]
p04 = params[:par_04]
Run Code Online (Sandbox Code Playgroud)
我得到这些值:
p01 => a
p02 => b
p03 =>
p04 =>
Run Code Online (Sandbox Code Playgroud)
换句话说,变量par_01和par_02将被扔掉,并在他们的地方将被移动par_03和par_04.
如何强制link_to接受带有nil值的参数?我正在考虑放置0s而不是nils然后在控制器动作中手动解析它,但这是一个非常难看的解决方案.