简体   繁体   English

如何从Rails中的另一个控制器获取帖子的ID?

[英]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_pathindex操作(复数)和带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.

相关问题 我如何获得我刚刚通过 form_with 创建的 model 的 ID,基本上从一个 controller 到 Rails 中的另一个 - How do i get the id of a model that i just created through form_with basically from one controller to another in Rails Rails 4如何从另一个控制器中获取ID - Rails 4 How to get id from another controller in form 我如何从 post 表单中获取值并在 rails controller 中使用它们 - How do i get values from a post form and use them in the rails controller 如何通过导轨将对象从一个控制器传递到另一个控制器? - How do I pass objects from one controller to another in rails? 如何在Rails 3中将Rails路由从controller /:id更改为controller /:name? - How do i change Rails routing from controller/:id to controller/:name in Rails 3? 如何从列表中的学生那里获得ID,并在另一页上显示学生的信息? Ruby on Rails - How do I get an id from a student in a list and display it the student's information on another page? Ruby on Rails Rails - 如何避免必须从另一个控制器中的create action触发一个控制器中的create操作 - Rails - how do I avoid having to trigger the create action in one controller from the create action in another controller 如何在Rails 3.1中将URL的格式设置为username / controller /:id? - How do I get the format of my URLs to be username/controller/:id in Rails 3.1? 如何在rails中获取帖子ID? - How to get the post id in rails? 如何使用Ajax向我的Rails控制器发出POST请求? - How do I make a POST request to my Rails controller with Ajax?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM