部分访问 Sinatra 变量
我对 Sinatra 相当陌生,我正在尝试从部分内部访问数据库中的数据。
这是我想要在页面上显示的部分的示例:
<% @articles.each do |article| %>
<ul>
<li> <%= article.articleName %> </li>
</ul>
<% end %>
如果我只是设置一个类似的路线
get '/articles' do
@article = Articles.all
erb :articles
end
和 /articles 页面,其中包含类似的内容
<% @articles.each do |article| %>
<article>
<p> <%= article.articleName %> </p>
<p> <%= article.articleBody %> </p>
</article>
<% end %>
,它就可以正常工作但是,如果我将上面的代码放入部分的。
任何帮助将不胜感激。我确信我错过了一些简单的事情。
I'm fairly new to Sinatra, and I'm trying to access data from a database from within a partial.
Here's an example of a partial that I want on a page:
<% @articles.each do |article| %>
<ul>
<li> <%= article.articleName %> </li>
</ul>
<% end %>
It works fine if I just set up a route like
get '/articles' do
@article = Articles.all
erb :articles
end
and the /articles page with something like
<% @articles.each do |article| %>
<article>
<p> <%= article.articleName %> </p>
<p> <%= article.articleBody %> </p>
</article>
<% end %>
However, it doesn't seem like the above code works if I put it into a partial.
Any help would be appreciated. I'm sure I'm missing something simple.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Sinatra 没有像 Rails 那样内置部分,但您可以使用普通模板作为部分,如以下所述: http://www.sinatrarb.com/faq.html#partials
示例:
文章模板:
partials/_article 模板:
PS:设置从模板根目录到partial 的路径。这种奇怪的语法
:'partials/_article'
是 Sinatra 的技巧,它使您能够访问子目录中的模板,这是行不通的(我认为)::partials/_article
或'partials/_article'
。Sinatra does not have built-in partials like Rails, but you can use ordinary templates as partials, as mentioned in: http://www.sinatrarb.com/faq.html#partials
Example:
articles template:
partials/_article template:
PS: set a path to partial from template root dir. This weird syntax
:'partials/_article'
is a Sinatra trick, it enables you to access template in subdir, this wouldn't work (I think)::partials/_article
or'partials/_article'
.Sinatra 没有内置部分功能。所以你有两个选择:
Sinatra has no partial functionality built-in. So you have two options: