简体   繁体   中英

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

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM