0000
作家
作家
  • 铜币143枚
  • 威望34点
  • 贡献值1点
阅读:1811回复:0

Ruby on Rails: 给link_to加上css属性

楼主#
更多 发布于:2007-08-07 08:40
Ruby on Rails: 给link_to加上css属性

原文地址: http://color-magic.cn/archives/59

没有人会所有的链接都长得一样吧:)至少我不会

普通的link_to是这样写的:
<%= link_to '编辑', :action => 'edit', :id => ablumn_photo %>


照瓢画葫芦,写
<%= link_to '编辑', :action => 'edit', :id => ablumn_photo, :class='link_button' %>


非常不幸,我失败了
找文档,发现有描述如下:
link_to(name, options = {}, html_options = nil, *parameters_for_method_reference)
Creates a link tag of the given name using a URL created by the set of options. See the valid options in the documentation for ActionController::Base#url_for. It‘s also possible to pass a string instead of an options hash to get a link tag that uses the value of the string as the href for the link. If nil is passed as a name, the link itself will become the name.

The html_options will accept a hash of html attributes for the link tag. It also accepts 3 modifiers that specialize the link behavior.

* :confirm => ‘question?‘: This will add a JavaScript confirm prompt with the question specified. If the user accepts, the link is processed normally, otherwise no action is taken.
* :popup => true || array of window options: This will force the link to open in a popup window. By passing true, a default browser window will be opened with the URL. You can also specify an array of options that are passed-thru to JavaScripts window.open method.
* :method => symbol of HTTP verb: This modifier will dynamically create an HTML form and immediately submit the form for processing using the HTTP verb specified. Useful for having links perform a POST operation in dangerous actions like deleting a record (which search bots can follow while spidering your site). Supported verbs are :post, :delete and :put. Note that if the user has JavaScript disabled, the request will fall back to using GET. If you are relying on the POST behavior, your should check for it in your controllers action by using the request objects methods for post?, delete? or put?.

You can mix and match the html_options with the exception of :popup and :method which will raise an ActionView::ActionViewError exception.


这么大一段,我可懒得看……只要这一行就可以了:
link_to(name, options = {}, html_options = nil, *parameters_for_method_reference)


第三个参数html_options,默认为nil,我也明明传进去了一个class呀,其实问题就出在ruby这东西可怕的省略上,第二个参数 options是一个hash,但是在我们的例子中它省略了花括号{},这样link_to就把class也算作了options的参数(这个强盗!我们需要限制一下它的活动范围),解决也很简单,给第二个参数加上{}即可,如:
<%= link_to '下一步: 选择模板', {:action => 'choose_template'}, :class=>'link_button' %>


顺便,参考一下link_to的源码:
1. def link_to(name, options = {}, html_options = nil, *parameters_for_method_reference)
   2.   if html_options
   3.     html_options = html_options.stringify_keys
   4.     convert_options_to_javascript!(html_options)
   5.     tag_options = tag_options(html_options)
   6.   else
   7.     tag_options = nil
   8.   end
   9.   url = options.is_a?(String) ? options : self.url_for(options, *parameters_for_method_reference)" <a href=\"#{url}\"#{tag_options}>#{name || url}</a>"
  10. end
.--. |o_o | |:_/ | // \ \ (| | ) /'\_ _/`\ \___)=(___/
游客

返回顶部