我是铁杆新手,最近在为学校项目工作时发现了我的一个节目观点出错。该错误与名称方法尚未定义有关我还不确定如何解决这个问题。非常感谢帮助!我收到的错误是:
progressBar.type = YLProgressBarTypeRounded;
progressBar.progressTintColor = [UIColor greenColor];
progressBar.stripesOrientation = YLProgressBarStripesOrientationVertical;
progressBar.stripesDirection = YLProgressBarStripesDirectionLeft;
相关文件包括..
主题/ show.html.erb
NoMethodError in Topics#show
Showing /Users/Jason/code/bloccit/app/views/topics/show.html.erb where line #17 raised:
undefined method `name' for nil:NilClass
<%= link_to post.title, [@topic, post] %>
</h4>
<small>
submitted <%= time_ago_in_words(post.created_at) %> ago by <%= post.user.name %><br>
<%= post.comments.count %> Comments
</small>
</div>
post.rb
<h1><%= @topic.name %></h1>
<% if policy(@topic).update? %>
<%= link_to "Edit Topic", edit_topic_path, class: 'btn btn-success' %>
<% end %>
<div class="row">
<div class="col-md-8">
<p class="lead"><%= @topic.description %></p>
<% @posts.each do |post| %>
<div class="media">
<div class="media-body">
<h4 class="media-heading">
<%= link_to post.title, [@topic, post] %>
</h4>
<small>
submitted <%= time_ago_in_words(post.created_at) %> ago by <%= post.user.name %><br>
<%= post.comments.count %> Comments
</small>
</div>
</div>
<% end %>
</div>
<div class="col-md-4">
<% if policy(Post.new).create? %>
<%= link_to "New Post", new_topic_post_path(@topic), class: 'btn btn-success' %>
<% end %>
</div>
</div>
topic.rb
class Post < ActiveRecord::Base
has_many :comments
belongs_to :user
belongs_to :topic
default_scope { order('created_at DESC') }
end
posts_controller.rb
class Topic < ActiveRecord::Base
has_many :posts
end
topics_controller.rb
class PostsController < ApplicationController
def show
@post = Post.find(params[:id])
@topic = Topic.find(params[:topic_id])
authorize @post
end
def new
@topic = Topic.find(params[:topic_id])
@post = Post.new
authorize @post
end
def create
@topic = Topic.find(params[:topic_id])
@post = Post.new(post_params)
@post.topic = @topic
authorize @post
if @post.save
flash[:notice] = "Post was saved."
redirect_to [@topic, @post]
else
flash[:error] = "There was an error saving the post. Please try again."
render :new
end
end
def edit
@topic = Topic.find(params[:topic_id])
@post = Post.find(params[:id])
authorize @post
end
def update
@topic = Topic.find(params[:topic_id])
@post = Post.find(params[:id])
authorize @post
if @post.update_attributes(post_params)
flash[:notice] = "Post was updated."
redirect_to [@topic, @post]
else
flash[:error] = "There was an error saving the post. Please try again."
render :new
end
end
private
def post_params
params.require(:post).permit(:title, :body)
end
end
答案 0 :(得分:4)
您在创建帖子时未设置user
,我不确定您的authorize
方法的实施情况。但是,
它应该像
#assuming you have the user as `current_user`
class PostsController < ApplicationController
...
def create
@topic = Topic.find(params[:topic_id])
@post = Post.new(post_params)
@post.user = current_user
@post.topic = @topic
...
end
...
end