如何在 Rails 视图中创建指向特定 Post 对象的链接?

发布于 2024-08-12 06:48:50 字数 166 浏览 4 评论 0原文

我是 Rails 新手,所以请原谅任何简单的问题。我已经开发了一个博客,并且正在做一些定制。只是好奇,如果我想在我的 index.html.erb 页面上呈现特定的帖子是可能的。例如,如果我创建一个帖子,标题:Cool Post,并且表中的 post_id 为 25,那么在我的索引页面中我可以调用要呈现的特定帖子吗?

I am new to rails so excuse any easy questions. I have developed a blog and I am doing some customization. Just curious, if I want to render a specific post on my index.html.erb page is that possible. For instance if I create a post, title: Cool Post and it has a post_id of 25 in the table, in my index page can I call that specific post to be rendered?

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

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

发布评论

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

评论(3

々眼睛长脚气 2024-08-19 06:48:50

很难准确地说出你的系统是如何设置的,但在 Rails 中“正确”的方法是在你的页面的 Controller 中执行类似的操作

def index
   @post = Post.find(25)

   ... Your other code here ...
end

: html.erb 你可以输出帖子:

<h2><%= h(@post.title) %></h2>
<%= h(@post.body) %>

当然,我完全猜测你的 @post 上的字段

编辑: 如果你想做的只是渲染@post 您仍然希望检索要在控制器中渲染的帖子,但随后您将希望使用部分内容并从 index.html.erb 中渲染它> 文件:

  1. 假设,在您的 app/views/posts/ 目录中创建一个 _post.html.erb 文件,并在其中放置您想要的任何渲染代码(或者从现有的 app/views/posts/index.html.erb 文件中抽象它:_注意:使用局部变量 post 而不是 引用部分中的帖子。 @post 以获得最大的灵活性。

  2. 然后在您的 index.html.erb 中调用它,如下所示:

    '帖子/帖子', :object => @post%>

此调用假设 index.html.erb 文件与 _post 不在同一目录中.html.erb 部分。

Its hard to say exactly how your system is setup, but the "correct" way to do it in Rails, is to do something like in the Controller for your page:

def index
   @post = Post.find(25)

   ... Your other code here ...
end

And in your index.html.erb you could output the post:

<h2><%= h(@post.title) %></h2>
<%= h(@post.body) %>

Of course I am completely guessing on the fields on your @post

Edit: If all you want to do is render the @post you will still will want to retrieve the post to render in the controller, but then you will want to use a partial and render it from the index.html.erb file:

  1. Hypothetically, create a _post.html.erb file in your app/views/posts/ directory, and place whatever rendering code you want there (or abstract it from the existing app/views/posts/index.html.erb file. _Note: refer to the post in the partial using a local variable post not @post for greatest flexibility.

  2. Then call it in your index.html.erb for like this:

    'posts/post', :object => @post %>

This call assumes the index.html.erb file is not in the same directory as the _post.html.erb partial.

寂寞美少年 2024-08-19 06:48:50

我认为他的问题是关于渲染/重定向的。
您应该执行(作为控制器中的最后一行)

render @post

redirect_to @post

第一个显示帖子,第二个在创建/更新后显示帖子。

I think his question is about rendering/redirecting.
You should do (as a last line in your controller)

render @post

or

redirect_to @post

First one to show a post, second one to show post after creation/update.

顾挽 2024-08-19 06:48:50

虽然 dcneiner 的解决方案在技术上是正确的,但 Rails 的方法并不是对这些东西进行硬编码。博客投入运行后,您将必须直接修改源代码才能更改此帖子。不建议这样做。

我建议您在帖子表中添加一个额外的列,以指示帖子是否应永久显示在首页上。这使您可以动态更新您的永久帖子,只需在创建永久帖子时打勾即可。

例如:

class Post < ActiveRecord
  named_scope :featured, :conditions => {:featured => true},
   :order => "created_at DESC"
end

Post.featured 将返回您的精选帖子以在您的首页上使用,按最新到最旧的顺序排列。

如果您一次只想要一篇精选帖子,您可以将以下内容添加到帖子模型中

  before_validation :highlander_clause

  def highlander_clause
    Post.featured.each{|p| p.update_attribute(:featured, false) if featured
  end

While dcneiner's solution is technically correct, the Rails way is not to hard code these things. Once the blog is operational you will have to modify the source directly to change this post. This is not advised.

I'm going to suggest you add an extra column to the Posts table to denote whether a Post should be permanently featured on the front page. This allows you to dynamically update your permanent posts with nothing more than a checkmark when you create it.

Eg:

class Post < ActiveRecord
  named_scope :featured, :conditions => {:featured => true},
   :order => "created_at DESC"
end

Post.featured will return your featured post for use on your front page, ordered newest to oldest.

If you only want one featured post at a time you can add the following to the Post model

  before_validation :highlander_clause

  def highlander_clause
    Post.featured.each{|p| p.update_attribute(:featured, false) if featured
  end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文