색인 레이아웃 페이지에서 내 db의 differents 게시물을 반복하는 동안 500 오류가 발생했습니다.
다음은 로그입니다.
Started GET "/posts" for 127.0.0.1 at 2020-07-18 22:23:44 +0200
Processing by PostsController#index as HTML
Rendering posts/index.html.erb within layouts/application
Post Load (0.5ms) SELECT "posts".* FROM "posts"
↳ app/views/posts/index.html.erb:14
Rendered posts/index.html.erb within layouts/application (Duration: 1923.6ms | Allocations: 18335)
Completed 500 Internal Server Error in 7372ms (ActiveRecord: 0.5ms | Allocations: 37272)
컨트롤러는 다음과 같습니다.
class PostsController < ApplicationController
before_action :find_post, only: %i[show edit update destroy]
def index
@posts = Post.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @posts }
end
end
def show; end
def new
@post = Post.new(post_params)
if @post.save
redirect_to @post, notice: "The post was created!"
else
render ‘new’
end
end
def create
@post = Post.new(post_params)
if @post.save
redirect_to @post, notice: "The post was created!"
else
render ‘new’
end
end
def edit; end
def update
if @post.update(post_params)
redirect_to @post, notice: "Update successful"
else
render ‘edit’
end
end
def destroy
@post.destroy
redirect_to root_path, notice: "Post destroyed"
end
private
def post_params
params.require(:post).permit(:title, :content, :category_id)
end
def find_post
@post = Post.find(params[:id])
end
end
내 게시물에 대한 내 db 항목은 다음과 같습니다.
내 인덱스보기 페이지 코드는 다음과 같습니다.
<!DOCTYPE html>
<html>
<head>
<title>Blog</title>
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
</head>
<body>
<% @posts.each do |post| %>
<h1><%= link_to "post.title", post.title %></h1>
<p><%= post.content %></p>
<% end %>
</body>
</html>
내가 루비로 시작하고 있다는 점에 유의하십시오 (개인용).
미리 감사드립니다. 언제든지 질문하거나 필요한 경우 리소스를 요청하세요.
먼저 컨트롤러 코드를 약간 정리해야합니다. Java 배경에서 온 것 같습니다. 제거해야합니다.;
class PostsController < ApplicationController
before_action :find_post, only: %i[show edit update destroy]
def index
@posts = Post.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @posts }
end
end
def show
end
def new
@post = Post.new(post_params)
if @post.save
redirect_to @post, notice: "The post was created!"
else
render 'new'
end
end
def create
@post = Post.new(post_params)
if @post.save
redirect_to @post, notice: "The post was created!"
else
render 'new'
end
end
def edit
end
def update
if @post.update(post_params)
redirect_to @post, notice: "Update successful"
else
render 'edit'
end
end
def destroy
@post.destroy
redirect_to root_path, notice: "Post destroyed"
end
private
def post_params
params.require(:post).permit(:title, :content, :category_id)
end
def find_post
@post = Post.find(params[:id])
end
end
%i
이전 작업 의 구문도 나에게 약간 외계인입니다. 일반적으로 기호를 사용할 것으로 예상합니다.
before_action :find_post, only: [:show :edit :update :destroy]
그러나 주요 문제는
보기 폴더에는 레이아웃 폴더가 있어야합니다. 그 안에 application.html.erb 템플릿이 있어야합니다. doctype, head 및 body 섹션이 거기에 있어야합니다. 이것은 당신의 오류의 원인이 아닙니다.
귀하의 PostsController
상속 ApplicationController
원하는 경우 사용되는 레이아웃의 이름을 설정하는,이 컨트롤러별로 레이아웃 지시어를 오버라이드 (override) 할 수있다
레이아웃 yield
에서 body
태그 내부 에 명령문 이 있어야합니다 . 그러면 렌더링되는 뷰 템플릿의 내용이 마술처럼 표시됩니다. 이것은 머리글과 바닥 글을 렌더링하기에 완벽한 장소이므로 하나의 머리글 뷰 부분과 하나의 바닥 글 뷰 부분 만 있습니다. 모든 페이지에서 렌더링됩니다. 이해가 되길 바랍니다.
따라서 템플릿에는 렌더링되는 뷰에 특정한 코드 만 포함되어야합니다.
<% @posts.each do |post| %>
<h1><%= link_to "post.title", post.title %></h1>
<p><%= post.content %></p>
<% end %>
오류는 게시물 제목 링크의 오류입니다.
<h1><%= link_to "post.title", post.title %></h1>
link_to
는 첫 번째 매개 변수는 텍스트가 귀하의 경우 어떤 표시 될 텍스트 문자열 값입니다으로 도우미 걸립니다 post.title
당신이 경우에 그 사용을 할 fsdfsd되는 게시물의 실제 제목 보여 그 변경해야하므로 post.title
를 얻을 수를 필드의 값
그래서 당신은 이제
<h1><%= link_to post.title %></h1>
하지만 실제 포스트 레코드의 쇼 액션이어야한다고 생각하는 링크 URL이 누락되었습니다. 이를 얻으려면 인스턴스 변수를 사용하면 rails가 경로를 추출 할 것입니다.이 경우에는 post
obect이므로 결국
<h1><%= link_to post.title, post %></h1>
오, 다른 사람들이 제안한대로 컨트롤러의 render 문에 대한 quiotes를 변경하십시오.
// Edit1 : '새로운'함수에 대한 '따옴표를'따옴표로 변경했습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다