Ruby on rails rails中的params[:id]来自哪里?

Ruby on rails rails中的params[:id]来自哪里?,ruby-on-rails,params,Ruby On Rails,Params,我是Rails的初学者。我现在正在学习rails的《开始rails 4》。我想问一下传递给params方法的“parameter”。以下是典型的rails控制器之一 class CommentsController < ApplicationController before_action :load_article def create @comment = @article.comments.new(comment_params) if @comment.sav

我是Rails的初学者。我现在正在学习rails的《开始rails 4》。我想问一下传递给params方法的“parameter”。以下是典型的rails控制器之一

class CommentsController < ApplicationController
  before_action :load_article
  def create
    @comment = @article.comments.new(comment_params)
    if @comment.save
      redirect_to @article, notice: 'Thanks for your comment'
    else
      redirect_to @article, alert: 'Unable to add comment'
    end
  end

  def destroy
    @comment = @article.comments.find(params[:id])
    @comment.destroy
    redirect_to @article, notice: 'Comment Deleted'
  end

  private
    def load_article
      @article = Article.find(params[:article_id])
    end

    def comment_params
      params.require(:comment).permit(:name, :email, :body)
    end
  end 
它通过find(params[:id])查找与文章关联的注释。我的问题是,params[:id]究竟来自哪里

它来自URL吗?还是rails会在创建任何注释记录时自动保存参数散列?所以我们可以通过find(params[:id])找到任何注释

装入物品的方法与此类似

def load_article
  @article = Article.find(params[:article_id])
end

它通过params[:article_id]查找文章。这个参数[:article_id]来自哪里?rails是如何通过它找到文章的?

参数[:id]
来自URL。当您在routes文件中使用
资源时,Rails将自动为您生成标准REST路由。在您的
destroy
示例中,通常是使用
DELETE
HTTP方法请求
/comments/:id
,其中
:id
被添加到
参数
散列中,即
参数[:id]
参数[:id]
是唯一标识(RESTful)的字符串Rails应用程序中的资源。它位于URL中资源名称之后

例如,对于一个名为
my_model
的资源,
GET
请求应该对应于类似
myserver.com/my_model/12345
的URL,其中
12345
是标识
my_model
的特定实例的
参数[:id]
。其他HTTP请求(PUT、DELETE等)及其RESTful对应项也类似


如果您仍然对这些概念和术语感到困惑,您应该阅读RESTful体系结构及其解释。

它来自URL,也许阅读的路由部分将帮助您理解它
def load_article
  @article = Article.find(params[:article_id])
end