开发者

Creating Link with Ruby on Rails

Obviously, I am new to this.

I need to make a link using rails to something within the model. The code I have is this. It is probably not even the correct syntax:

<li id="nav_home"><%= link_to 'SOMETHING', {:controller => 'inventor开发者_C百科ies', :action => 'home'} %></li>

This code defaults to creating a text link, but I want the link element to link. Ideally it would output as so:

<li><a href="goes-to-something"></a></li>

Thanks!


The whole point of link_to is that it helps you create links to resources within your application via your routes. If all you want is <li><a href="#"></a></li>, then just use <li><a href="#"></a></li> --- no need to involve link_to.


<li><%= link_to '', '#' %></li>

But that's a bit nutty. Plain HTML is fine. Or there is link_to_function which will create a link like that but have an onclick that executes some javascript.

Or you can call other helpers in place of your "SOMETHING"

<li><%= link_to image_tag('foo.png'), '#' %></li>

Renders:

<li><a href="#"><img src="/images/foo.png"/></a></li>


If you are using JavaScript to make this empty link do something, you don't actually need a link - you can place an event on any HTML element and handle if accordingly.


I think you want to look at Rail's RESTful named routes, which provide the various "inventories_path" and "inventories_url" functions. If, in your routes.rb, you have "map.resources :inventories" then you get all these functions for free.

Check out the Rails Guide on Routing. Then you can do something like:

link_to "SOMETHING", inventory_url(@myinventory)
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜