0

又是 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>

嘿,伙计们,我感到很抱歉;后睡着了。

在查看我所有的控制器时,我并没有特别看到它是在哪里定义的,但是它在我开始最后一个任务之前确实有效,所以我不知道中间发生了什么。

这是用户控制器

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'

结束结束

micropost_controller

class MicrpostsController< ApplicationController
before_filter :signed_in_user, only: [:create, :destroy]

定义索引

结尾

 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
4

1 回答 1

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(user)followed_user_ids =“从关系中选择follower_id =:user_id” where(“user_id IN(#{followed_user_ids})或user_id =:user_id”,user_id:user.id)结束

于 2013-03-14T17:53:49.310 回答