Ruby on rails Rails无法检索保存在DB中的数据
在RubyonRails中,我希望将数据保存在DB中,并在页面上显示保存的内容,但是我得到了一个错误,如下图所示。 我担心了三天,但还是解决不了。我想知道目标方法Ruby on rails Rails无法检索保存在DB中的数据,ruby-on-rails,Ruby On Rails,在RubyonRails中,我希望将数据保存在DB中,并在页面上显示保存的内容,但是我得到了一个错误,如下图所示。 我担心了三天,但还是解决不了。我想知道目标方法 class ArticlesController < ApplicationController # before_action :authenticate_user! before_action :find_article, only: [:show, :edit, :update, :destroy] def
class ArticlesController < ApplicationController
# before_action :authenticate_user!
before_action :find_article, only: [:show, :edit, :update, :destroy]
def index
@articles = Article.order(created_at: :desc)
end
def show
end
def new
@article = Article.new
end
def edit
end
def create
@article = Article.new(article_params)
if @article.save
redirect_to @article, notice: ""
else
render :new, alert: ""
end
end
def update
if @article.update(article_params)
redirect_to @article, notice: ""
else
render :edit, alert: ""
end
end
def destroy
if @article.destroy
redirect_to root_path, notice: ""
else
redirect_to root_path, alert: ""
end
end
private
def find_article
@article = Article.find(params[:id])
end
def article_params
params.require(:article).permit(:title, :body)
end
end
当您编写参考资料:文章时,它将自动生成所有7条路线 所以,您可以像这样更新路由文件
Rails.application.routes.draw do
# root "tops#index"
resources :articles
end
您面临的问题是,您没有发送有效的ID。您正在使用默认情况下将生成7条
路由的资源:文章。但随后使用覆盖显示
get "/articles/index", to: "articles#index"
get "/articles/show", to: "articles#show"
当route
将get”/articles/index>发送到:“articles#index”
时,它假设index
是id
,并将转到的“articles#show”
,并且无法找到'id'
和'index'
,因此
路线
应如下所示
get '/articles', to: 'articles#index'
get '/articles/new', to: 'articles#new'
get '/articles/:id', to: 'articles#show'
post '/articles', to: 'articles#create'
delete 'articles/:id', to: 'articles#destroy'
get '/articles/:id/edit', to: 'articles#edit'
put '/articles:id', to: 'articles#update'
没有
resources :articles
get "/articles/index", to: "articles#index"
get "/articles/new", to: "articles#new"
get "/articles/show", to: "articles#show"
在您的情况下,或者您可以简单地使用
resources:articles
将自动生成所有7条路线
因此,请删除这些路由
get "/articles/index", to: "articles#index"
get "/articles/new", to: "articles#new"
get "/articles/show", to: "articles#show"
get "/articles/index", to: "articles#index"
get "/articles/new", to: "articles#new"
get "/articles/show", to: "articles#show"