In Rails 3, how can you make a div a button (a clickable div that links to a new page)

﹥>﹥吖頭↗ 提交于 2019-12-20 15:24:17

问题


I know that there are options to do this manually, such as here : How do you make the entire DIV clickable to go to another page?

However, in Rails 3 we make links like this: <%= link_to "name", url %>

And I am wondering -- is there a proper Rails way to make a whole div a button. I realize I could make a button. However, let's say I want to fill it with content like text and a picture - then how would I make that whole div clickable in a rails way?

For example: <%= @story.title %> <%= @story.blurb %>

In this example, I would want to make #story clickable, with the rails generated content that I specified.. Any ideas?


回答1:


For those interested, the answer is:

 <div class="class_name">
 <%= link_to content_tag(:span, "Text for the LInk"), route %>
 </div>

And then, in the CSS set .class_name to position:relative; and:

 .class_name span {
width:100%;
height:100%;
position:absolute;
z-index:2;
top:0px;
left:0px;
 }

The link will now assume the size of its parent container, and thus give the impression the whole container is a link.




回答2:


I think that button_to is what you need :

http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to




回答3:


I might use jquery if you really wanted to do this

$(document).ready(function() {
    $('#somediv').click(function(event){
        document.location = 'http://someplace.com';
    });
});

You can't make a "clickable" raw div like that in a rails way... the concept doesn't make any sense. That's just not how it works

(added missing close parens for click)

not tested



来源:https://stackoverflow.com/questions/6804848/in-rails-3-how-can-you-make-a-div-a-button-a-clickable-div-that-links-to-a-new

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!