添加额外的选项 link_to do
Add extra options to link_to do
我想用这个 link_to 助手传递一个参数。我该怎么做?
<%= link_to ('/my_controller/my_action') do %>
<div>haha</div>
<% end %>
原代码我用的是标准文字link:
<%= link_to("link text", {:controller => 'my_controller', :action => 'my_action', :id => my_id}) %>
根据我的理解,您想在使用块时将选项传递给 link_to
助手。
来自 http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
的文档
可以使用以下格式
link_to(options = {}, html_options = {}) do
# name
end
举个例子,使用你提供的代码:
<%= link_to({:controller => 'my_controller', :action => 'my_action', :id => my_id}) do %>
<div>haha</div>
<% end %>
就像你的第二个例子:
<%= link_to({:controller => 'my_controller', :action => 'my_action', :id => my_id}) do %>
My text goes here
<% end %>
如果你有一个路径助手(即如果它类似于 PostsController
的 show
动作)那么你可以这样做:
<%= link_to post_path(my_id) do %>
My text goes here
<% end %>
我想用这个 link_to 助手传递一个参数。我该怎么做?
<%= link_to ('/my_controller/my_action') do %>
<div>haha</div>
<% end %>
原代码我用的是标准文字link:
<%= link_to("link text", {:controller => 'my_controller', :action => 'my_action', :id => my_id}) %>
根据我的理解,您想在使用块时将选项传递给 link_to
助手。
来自 http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
的文档可以使用以下格式
link_to(options = {}, html_options = {}) do
# name
end
举个例子,使用你提供的代码:
<%= link_to({:controller => 'my_controller', :action => 'my_action', :id => my_id}) do %>
<div>haha</div>
<% end %>
就像你的第二个例子:
<%= link_to({:controller => 'my_controller', :action => 'my_action', :id => my_id}) do %>
My text goes here
<% end %>
如果你有一个路径助手(即如果它类似于 PostsController
的 show
动作)那么你可以这样做:
<%= link_to post_path(my_id) do %>
My text goes here
<% end %>