我的Rails博客引擎有问题。总的来说,帖子的注册和创建工作(它们在SQLite数据库中形成),但是当我尝试列出用户创建的所有帖子时,它会在用户的show.html.erb视图中抛出NoMethodError:undefined method '标题'
PostsController.rb
class PostsController < ApplicationController
before_action :user_is_required, only: [:create, :destroy]
def index
@posts = Post.all
end
def new
@post = Post.new
end
def create
@post = user_logged_in.posts.create(post_params)
if @post.save
flash[:success] = "Your post has been successfully saved."
redirect_to root_path
else
flash[:danger] = "Oops! Something went wrong. Try again."
redirect_to new_post_path
end
end
def edit
end
def update
if @post.update_attributes(post_params)
flash[:success] = "Your post has been successfully updated."
redirect_to post_path(@posts)
else
flash[:danger] = "Oops! Something went wrong. Try again."
render 'post'
end
end
def show
@post = Post.find(params[:id])
end
def destroy
if @post.destroy
flash[:success] = "Your post has been successfully deleted."
redirect_to posts_path
else
flash[:danger] = "Oops! Something went wrong. Try again."
end
end
private
def post_params
params.require(:post).permit(:title, :content)
end
end
show.html.erb (用户观点)
<div class="posts">
<div class="container marketing">
<div class="col-lg-4 col-md-4 col-xs-12 col-sm-12">
<section id="info">
<%= image_tag("#", :class => "user_avatar") %>
<h2 class="user_name">
<%= user_logged_in.name %>
</h2>
</section>
<% if user_logged_in %>
<%= link_to "Change avatar", class: "btn btn-primary" %><br>
<% end %>
<%= link_to "Follow", class: "btn btn-primary" %>
</div>
<div class="col-lg-8 col-md-8 col-sm-12 col-xs-12">
<h2>
<%= user_logged_in.name %> has <%= user_logged_in.posts.count %> posts.
</h2>
<% if user_logged_in.posts.any? %>
<% @posts.each do |post| %>
<h2 class="title">
<%= user_logged_in.posts.title %> # The line which raises the error
</h2>
<p class="post">
<%= user_logged_in.posts.content %>
</p>
<% end %>
<% end %>
</div>
</div>
post.rb
class Post < ActiveRecord::Base
belongs_to :user
default_scope -> { order(created_at: :desc) }
validates :user_id, presence: true
validates :title, presence: true
validates :content, presence: true
end
我是Ruby on Rails的新手,这是我的第一个项目。任何和所有帮助将受到高度赞赏。
提前致谢。
答案 0 :(得分:0)
posts
应该是post
因为你在内部循环,<%= user_logged_in.posts.title %>
应该是<%= post.title %>
:
<% @posts.each do |post| %>
<h2 class="title">
<%= post.title %> # The line which raises the error
</h2>
<p class="post">
<%= post.content %>
</p>
<% end %>
答案 1 :(得分:0)
认为逻辑在这里有点混乱。
user_logged_in.posts
是一种关系,因此它会返回与用户登录相关联的帖子。
@posts
设置了表格中的所有帖子。
<% if user_logged_in.posts.any? %>
<% @posts.each do |post| %>
<h2 class="title">
<%= user_logged_in.posts.title %> # The line which raises the error
</h2>
<p class="post">
<%= user_logged_in.posts.content %>
</p>
<% end %>
<% end %>
这表示如果登录用户有任何帖子,循环遍历所有帖子(不仅仅是用户帖子),然后我认为你想要显示用户帖子,但你出错了句法。尝试更改代码,如下所示:
<% if user_logged_in.posts.any? %>
<% user_logged_in.posts.each do |post| %>
<h2 class="title">
<%= post.title %>
</h2>
<p class="post">
<%= post.content %>
</p>
<% end %>
<% end %>
这将遍历用户帖子,将每个帖子分配到post
变量,然后显示该帖子的标题和内容。