新手再次出现在Rails中。按照Michael Hartl关于rails的教程,我试图通过最后一节,我得到了这个错误
undefined method `id' for #<String:0x3794b80>
它来自我的_feed_item.html.erb,我不明白为什么我会收到此错误。
<li id="<%= feed_item.id %>">
<%= link_to gravatar_for(feed_item.user), feed_item.user %>
<span class="user">
<%= link_to feed_item.user.name, feed_item.user %>
</span>
<span class="content"><%= feed_item.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(feed_item.created_at) %> ago.
</span>
<% if current_user?(feed_item.user) %>
<%= link_to "delete", feed_item, method: :delete, data: { confirm: "You sure?" }, title: feed_item.content %>
<% end %>
</li>
嘿伙计们,对不起,我觉得;睡着了。
在查看我的所有控制器时,我并没有特别看到它的定义,但是在我开始上一个任务之前它确实有效,所以我不知道它们之间发生了什么。
这是user_controller
class UsersController < ApplicationController
before_filter :signed_in_user, only: [:index, :edit, :update]
before_filter :correct_user, only: [:edit, :update]
before_filter :admin_user, only: :destroy
def new
@user=User.new
end
def create
@user=User.new(params[:user])
if @user.save
#Handle a save
sign_in @user
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
else
render 'new'
end
end
def show
@user=User.find(params[:id])
@microposts=@user.microposts.paginate(page: params[:page])
end
def edit
@user=User.find(params[:id])
end
def update
@user=User.find(params[:id])
if @user.update_attributes(params[:user])
flash[:success]="Successfully updated your profile"
sign_in @user
redirect_to @user
else
render 'edit'
end
end
def correct_user
@user=User.find(params[:id])
redirect_to(root_path) unless current_user?(@user)
end
def index
@users=User.paginate(page: params[:page])
end
def destroy
User.find(params[:id]).destroy
flash[:success]="User destroyed"
redirect_to users_url
end
def admin_user
redirect_to(root_path) unless current_user.admin?
end
def following
@title="Following"
@user=User.find(params[:id])
@users=@user.followed_users.paginate(page: params[:page])
render 'show_follow'
end
def followers
@title="Followers"
@user=User.find(params[:id])
@users=@user.followers.paginate(page: params[:page])
render 'show_follow'
端 端
micrpost_controller
class MicrpostsController< ApplicationController
before_filter :signed_in_user, only: [:create, :destroy]
def index
端
def create
@micropost=current_user.microposts.build(params[:micropost])
if @micropost.save
flash[:success]="Micropost Created!"
redirect_to root_url
else
@feed_items = []
render 'static_pages/home'
end
end
def destroy
@micropost.destroy
redirect_to root_url
end
def correct_user
@micropost=current_user.microposts.find_by_id(params[:id])
redirect_to root_url if @micropost.nil?
end
end
答案 0 :(得分:0)
我得到了工作的人。问题出在我的微博模型中。
def self.from_users_followed_by(user)
followed_user_ids= "SELECT followed_id FROM relationships WHERE follower_id=:user_id",
where("user_id IN (#{followed_user_ids}) OR user_id=:user_id", user_id: user)
end
应该是 def self.from_users_followed_by(用户) followed_user_ids =“SELECT followed_id FROM relationships WHERE follower_id =:user_id” where(“user_id IN(#{followed_user_ids})OR user_id =:user_id”,user_id:user.id) 端