RoR link_to 一个不同的文件
RoR link_to a different file
我在我的帖子视图中创建了一个名为 lists.html.erb 的文件,它列出了所有不同的帖子。在我的 index.html.erb 中,它最多列出 5 个帖子。该按钮应该 link 到多个帖子,但我收到一条错误消息,说我有一个未定义的局部变量或方法。我正在尝试:
<%= link_to "All Posts", lists_path %>
和
<%= link_to "All Posts", lists %>
求一个问题
Prefix Verb URI Pattern Controller#Action
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
root GET / posts#index
GET /*path(.:format) redirect(301, /)
Posts_controller.rb
class PostsController < ApplicationController
before_action :find_post, only: [:show, :edit, :update, :destroy]
def index
@posts = Post.all.order('created_at DESC')
end
def new
@post = Post.new
if @post.save
redirect_to @post
else
render 'new'
end
end
def create
@post = Post.new(post_params)
if @post.save
redirect_to @post, notice: "Post was saved"
else
render 'new', notice: "I could not save the post. Call me for help if it keeps happening"
end
end
def show
#@post = Post.find(params[:id])
end
def edit
#@post = Post.find(params[:id])
end
def lists
@posts = Post.all.order('created_at DESC')
end
def update
@post = Post.find(params[:id])
if @post.update(params[:post].permit(:title, :body))
redirect_to @post
else
render 'edit'
end
end
def destroy
#@post = Post.find(params[:id])
if @post.destroy
redirect_to root_path
else
redirect_to post_path, notice: "I couldn't be deleted for some reason. Try again or contact me"
end
end
private
def post_params
params.require(:post).permit(:title, :body, :image, :slug)
end
def find_post
@post = Post.friendly.find(params[:id])
end
end
您的控制器中有该方法,但您没有为其放置路由:
def lists
@posts = Post.all.order('created_at DESC')
end
您可以添加一条简单的路线,例如
get 'lists' => 'posts#lists', :as => :lists
如果你 运行 rake routes
它会显示这条新路线 - lists_path
所以 <%= link_to "All Posts", lists_path %>
会起作用。
我在我的帖子视图中创建了一个名为 lists.html.erb 的文件,它列出了所有不同的帖子。在我的 index.html.erb 中,它最多列出 5 个帖子。该按钮应该 link 到多个帖子,但我收到一条错误消息,说我有一个未定义的局部变量或方法。我正在尝试:
<%= link_to "All Posts", lists_path %>
和
<%= link_to "All Posts", lists %>
求一个问题
Prefix Verb URI Pattern Controller#Action
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
root GET / posts#index
GET /*path(.:format) redirect(301, /)
Posts_controller.rb
class PostsController < ApplicationController
before_action :find_post, only: [:show, :edit, :update, :destroy]
def index
@posts = Post.all.order('created_at DESC')
end
def new
@post = Post.new
if @post.save
redirect_to @post
else
render 'new'
end
end
def create
@post = Post.new(post_params)
if @post.save
redirect_to @post, notice: "Post was saved"
else
render 'new', notice: "I could not save the post. Call me for help if it keeps happening"
end
end
def show
#@post = Post.find(params[:id])
end
def edit
#@post = Post.find(params[:id])
end
def lists
@posts = Post.all.order('created_at DESC')
end
def update
@post = Post.find(params[:id])
if @post.update(params[:post].permit(:title, :body))
redirect_to @post
else
render 'edit'
end
end
def destroy
#@post = Post.find(params[:id])
if @post.destroy
redirect_to root_path
else
redirect_to post_path, notice: "I couldn't be deleted for some reason. Try again or contact me"
end
end
private
def post_params
params.require(:post).permit(:title, :body, :image, :slug)
end
def find_post
@post = Post.friendly.find(params[:id])
end
end
您的控制器中有该方法,但您没有为其放置路由:
def lists
@posts = Post.all.order('created_at DESC')
end
您可以添加一条简单的路线,例如
get 'lists' => 'posts#lists', :as => :lists
如果你 运行 rake routes
它会显示这条新路线 - lists_path
所以 <%= link_to "All Posts", lists_path %>
会起作用。