[英]How do I get the ID of a post from another controller in Rails?
The main of my web is home/index. 我的网站主要是首页/索引。
And I want to add the contents of the posts controller to the main. 我想将posts控制器的内容添加到main中。 I would like to create a link from home/index to posts/show.
我想创建一个从首页/索引到帖子/节目的链接。 Also I am using scaffold.
我也在用脚手架。
But I am getting the error Could not find Post with 'id' = . 但是我收到错误消息找不到'id'=的帖子 。
What can I do? 我能做什么?
home_controller.rb home_controller.rb
def index
@post = Post.new
@posts = Post.find(params[:id])
end
posts_controller.rb posts_controller.rb
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
# GET /posts
# GET /posts.json
def index
@posts = Post.find(params[:id])
end
# GET /posts/1
# GET /posts/1.json
def show
end
# GET /posts/new
def new
@post = Post.new
end
# GET /posts/1/edit
def edit
end
# POST /posts
# POST /posts.json
def create
@post = Post.new(post_params)
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: @post }
else
format.html { render :new }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if @post.update(post_params)
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: @post }
else
format.html { render :edit }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
@post.destroy
respond_to do |format|
format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:title, :content)
end
end
index.html.erb (home_controller) index.html.erb(home_controller)
<%= link_to 'Show', post_path(@posts.id) %>
An index
action is usually used to represent a collection of items. index
操作通常用于表示项目的集合。 For example: 例如:
def index
@posts = Post.order(:published_at).all
end
Your index
action looks very different though. 但是,您的
index
操作看起来非常不同。 You're creating a new instance of a Post
, and then attempting to lookup a single Post
by ID. 您正在创建
Post
的新实例,然后尝试按ID查找单个Post
。 This combines the role of a new
action with a show
action. 这结合了
new
动作和show
动作的角色。 For example: 例如:
def new
@post = Post.new
end
def show
@post = Post.find(params[:id])
end
If you're using resourceful routing, your route helpers expect you to be using this pattern. 如果您使用的是资源丰富的路由,则路由助手希望您正在使用此模式。 Thus, a request to
/posts
goes to the PostsController#index, and /posts/1
goes to the PostsController#show action with an ID parameter equal to 1
. 因此,对
/posts
的请求转到PostsController#index,而/posts/1
转到ID参数等于1
的PostsController#show操作。
In your view, you would link to the index
action with posts_path
(plural) and the show
action with post_path(1)
(singular, where 1
is the ID of the Post). 在您的视图中,您将链接到带
posts_path
的index
操作(复数)和带post_path(1)
的show
操作(单数,其中1
是Post的ID)。
I recommend you review your code and adjust it to fit these best practices / the Rails way and see if you can resolve the error. 我建议您检查代码并对其进行调整以适合这些最佳实践/ Rails方式,并查看是否可以解决错误。
And I want to add the contents of the posts controller to the main.
我想将posts控制器的内容添加到main中。 I would like to create a link from home/index to posts/show.
我想创建一个从首页/索引到帖子/节目的链接。
Based on this, i believe you want to display all the posts on your home page and also have a link against each post to go to show page for that post. 基于此,我相信您希望在主页上显示所有帖子,并且还具有针对每个帖子的链接以显示该帖子的页面。
You can achieve that by doing these simple changes in your home action and its view: 您可以通过在home操作及其视图中进行以下简单更改来实现此目的:
home_controller.rb home_controller.rb
def index
@posts = Post.all # Use pagination, if you have hundreds or thousands of posts
end
home/index.html.erb 家用/ index.html.erb
<% @posts.each do |post| %>
<%= post.title %>
<%= post.other_details %>
<!-- Link to visit individual post's show page -->
<%= link_to 'See more', post_path(post) %>
<% end %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.