开发者

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开发者_如何学C 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 think that button_to is what you need :

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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜