如何在 Rails 视图中创建指向特定 Post 对象的链接?
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
很难准确地说出你的系统是如何设置的,但在 Rails 中“正确”的方法是在你的页面的
Controller
中执行类似的操作: html.erb 你可以输出帖子:
当然,我完全猜测你的
@post
上的字段编辑: 如果你想做的只是渲染
@post
您仍然希望检索要在控制器中渲染的帖子,但随后您将希望使用部分内容并从index.html.erb
中渲染它> 文件:假设,在您的
app/views/posts/
目录中创建一个_post.html.erb
文件,并在其中放置您想要的任何渲染代码(或者从现有的app/views/posts/index.html.erb
文件中抽象它:_注意:使用局部变量post
而不是引用部分中的帖子。 @post
以获得最大的灵活性。然后在您的
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:And in your
index.html.erb
you could output the post: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 theindex.html.erb
file:Hypothetically, create a
_post.html.erb
file in yourapp/views/posts/
directory, and place whatever rendering code you want there (or abstract it from the existingapp/views/posts/index.html.erb
file. _Note: refer to the post in the partial using a local variablepost
not@post
for greatest flexibility.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.我认为他的问题是关于渲染/重定向的。
您应该执行(作为控制器中的最后一行)
或
第一个显示帖子,第二个在创建/更新后显示帖子。
I think his question is about rendering/redirecting.
You should do (as a last line in your controller)
or
First one to show a post, second one to show post after creation/update.
虽然 dcneiner 的解决方案在技术上是正确的,但 Rails 的方法并不是对这些东西进行硬编码。博客投入运行后,您将必须直接修改源代码才能更改此帖子。不建议这样做。
我建议您在帖子表中添加一个额外的列,以指示帖子是否应永久显示在首页上。这使您可以动态更新您的永久帖子,只需在创建永久帖子时打勾即可。
例如:
Post.featured 将返回您的精选帖子以在您的首页上使用,按最新到最旧的顺序排列。
如果您一次只想要一篇精选帖子,您可以将以下内容添加到帖子模型中
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:
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