小编akk*_*dio的帖子

%运算符在N%2中的Ruby中做了什么?

if counter % 2 == 1 我试图解码这一行 - 这是一个Rails项目,我试图弄清楚%这个if语句的作用.

ruby syntax

25
推荐指数
2
解决办法
6万
查看次数

如何使用content_for以便内容显示在布局中

我正在测试我的rails 3.2应用程序中的content_for并遵循rails指南,但它们是特定于实际文件的,我似乎无法获得收益:

application.html.erb文件:

 <!DOCTYPE html>
 <html>
<head>
 ...
</head>

<body>




<%= yield :navigation %> #shouldn't this load the content_for block named :navigation specified in the _main_nav.html.erb partial? 

 <%= yield %>  #this load the index page content


</body>
 </html>
Run Code Online (Sandbox Code Playgroud)

我创建了一个布局文件_main_nav.html.erb(我知道我可以使用<%= render'layouts/header'%>进行渲染,但我尝试使用content_for)_main_nav.html.erb是:

<% content_for :navigation do %>
<ul>
 <li>Home</li>
 </ul>

<% end %>
Run Code Online (Sandbox Code Playgroud)

他们以 这种方式阅读了RailsGuide http://guides.rubyonrails.org/layouts_and_rendering.html#using-the-content-for-method这应该有用.但事实并非如此.我没有收到错误.看似简单,但我很难过.

当我转到我的index.html.erb文件时,我希望看到这个结果:

content-for ruby-on-rails-3

7
推荐指数
2
解决办法
1万
查看次数

为什么不能耙路线更有帮助

当您在shell中键入rake路由时,它会显示一个很好的路由列表:

      new_edition GET    /editions/new(.:format)           editions#new
     edit_edition GET    /editions/:id/edit(.:format)      editions#edit
         edition GET    /editions/:id(.:format)           editions#show
                 PUT    /editions/:id(.:format)           editions#update
              DELETE /editions/:id(.:format)           editions#destroy
Run Code Online (Sandbox Code Playgroud)

这非常有用,但为什么不显示需要在应用程序中使用的实际代码

 edition GET    /editions/:id(.:format)  editions#show  edition_path()
Run Code Online (Sandbox Code Playgroud)

我猜这是因为它可能还有更多,但是一般的问题是当我查看示例时给出的路径我查找了一个如何明确编码以了解路由意味着什么的示例...

routes ruby-on-rails link-to

0
推荐指数
1
解决办法
239
查看次数