帮助我了解 Sinatra 中的动态布局

发布于 2024-08-26 03:01:22 字数 933 浏览 2 评论 0原文

帮助我理解这一点;我正在学习 Sinatra(还有 Rails,呃,还有 Ruby)。

假设我正在开发一个搜索应用程序。搜索表单放置在一个 div 中,结果将放置在另一个 div 中。搜索表单通过先前的视图(可能来自登录表单)呈现到 div 中。

我想处理表单参数,执行搜索,并将结果呈现到结果 div 中。

如果我在布局中有一个“yield”并从不同的视图渲染 div,则结果 div 在渲染时会擦除搜索 div。

如果我在默认布局中定义 div,然后只渲染内容,显然布局会变得混乱:必须有两个“yields”,而且我不认为 Sinatra 支持将块传递给yields。

我尝试了 foca 的 sinatra-content-for 插件,这似乎更接近我需要的。但我不知道在哪里放置“yield_content”语句。

如果我的布局中有这个 haml:

#search
  -# search form
  = yield_content :search
#results
  -# search results
  = yield_content :results

... 在我的搜索视图中: ...

 - content_for :search do
 %form{:method => "post"... etc.

在结果视图中:

- content_for :results do
%table{:class => 'results'... etc.

这种方法有效,但是当我渲染结果视图时,搜索 div 被清空。我想保留它。我做错了什么吗?我应该如何设置这个?

Help me understand this; I'm learning Sinatra (and Rails for that matter, er, and Ruby).

Say I'm doing a search app. The search form is laid out in one div, and the results will be laid out in another. The search form is rendered into the div by a previous view (maybe from a login form).

I want to process the form params, perform the search, and render the results into the results div.

If I have a single "yield" in the layout and render the divs from different views, the results div erases the search div when it renders.

If I define the divs in the default layout, then just render the content, obviously the layout will be messed up: there would have to be two "yields" and I don't think Sinatra supports passing blocks in to yields.

I tried foca's sinatra-content-for plugin, and that seems closer to what I need. But I can't figure out where to place the "yield_content" statements.

If I have this haml in my layout:

#search
  -# search form
  = yield_content :search
#results
  -# search results
  = yield_content :results

... this in my search view:

 - content_for :search do
 %form{:method => "post"... etc.

... and this in the results view:

- content_for :results do
%table{:class => 'results'... etc.

This sort of works but when I render the results view, the search div is emptied out. I would like to have it remain. Am I doing something wrong? How should I set this up?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

御弟哥哥 2024-09-02 03:01:22

我认为你的意思是你总是想显示 2 个 div,但在新的搜索中它们应该是空的,并且在结果页面上它们应该被填充。您可能可以使用一个 haml 模板,然后根据请求方法不同地填充它:(

get "/search" do
    # render haml
end

post "/search" do
    # set instance variables: @search & @results
    # run search
    # render haml
end

抱歉,这是非常伪的......不是在真正的计算机上。)

I think you mean you ALWAYS want to show 2 divs, but in a new search they should be empty and on a results page they should be populated. You can probably get away w/ one haml template, and just populate it diff'tly on the request method:

get "/search" do
    # render haml
end

post "/search" do
    # set instance variables: @search & @results
    # run search
    # render haml
end

(Sorry this is very pseudo... not at a real computer.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文