Ruby on rails 未定义的方法'blog#u path';在轨
我试图将一个现有项目从sqlite3库更改为postgresql库,但最后一个错误我不知道如何解决 以下html文件中的blog_路径启动未定义的方法“blog_路径”错误Ruby on rails 未定义的方法'blog#u path';在轨,ruby-on-rails,Ruby On Rails,我试图将一个现有项目从sqlite3库更改为postgresql库,但最后一个错误我不知道如何解决 以下html文件中的blog_路径启动未定义的方法“blog_路径”错误 <!DOCTYPE html> <html> <head> <title>My Blog</title> <link href="https://stackpath.bootstrapcdn.com/bootstrap/
<!DOCTYPE html>
<html>
<head>
<title>My Blog</title>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
</head>
<body>
<h1>My Blogs</h1>
<div class="main">
<div class="left-block">
</div>
<div class="blog-container">
<% @blogs = @blogs.reverse %>
<% @blogs.each do |blog| %>
<div class="blog">
<p class="title"><%= blog.title %></p>
<p class="time"><%= blog.created_at %></p>
<%= link_to 'Learn More', blog_path(blog), class: "btn btn-info" %>
</div>
<% end %>
</div>
<div class="right-block">
</div>
</div>
<footer>
<%= link_to 'New Blog', '/blogs/new', class: "btn btn-primary" %>
</footer>
</body>
</html>
这是blogs_controller.rb
class BlogsController < ApplicationController
def index
@blogs = Blog.all
end
def new
@blog = Blog.new
end
def create
@blog = Blog.new(blog_params)
if @blog.save
redirect_to blogs_url
else
render 'new'
end
end
def show
@blog = Blog.find(params[:id])
end
def edit
@blog = Blog.find(params[:id])
end
def update
@blog = Blog.find(params[:id])
@blog.update_attributes(blog_params)
redirect_to blogs_url
end
def destroy
@blog = Blog.find(params[:id])
@blog.destroy
redirect_to blogs_url
end
private
def blog_params
params.require(:blog).permit(:title, :content)
end
end
class BlogsController
**请告诉我是否需要任何其他资源来解决此问题。您应该在路由文件中使用
资源:blogs
,而不是单独定义每个路由
这里有更多非常感谢!它修复了它。这是一个额外的问题,但是如果我想将url设置为“用户#索引”的“注册”,我需要显式地指定它,对吗?
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
get '/blogs' => 'blogs#index'
get '/blogs/new' => 'blogs#new'
post '/blogs' => 'blogs#create'
get '/blogs/:id' => 'blogs#show'
get '/blogs/:id/edit' => 'blogs#edit'
patch '/blogs/:id' => 'blogs#update'
delete '/blogs/:id' => 'blogs#destroy'
end
class BlogsController < ApplicationController
def index
@blogs = Blog.all
end
def new
@blog = Blog.new
end
def create
@blog = Blog.new(blog_params)
if @blog.save
redirect_to blogs_url
else
render 'new'
end
end
def show
@blog = Blog.find(params[:id])
end
def edit
@blog = Blog.find(params[:id])
end
def update
@blog = Blog.find(params[:id])
@blog.update_attributes(blog_params)
redirect_to blogs_url
end
def destroy
@blog = Blog.find(params[:id])
@blog.destroy
redirect_to blogs_url
end
private
def blog_params
params.require(:blog).permit(:title, :content)
end
end