2013-03-17 100 views
3

在我的Rails应用程序,我需要通过用户名列表中的用户,我得到的错误是设计 - 清单用户

NoMethodError in Posts#index 

Showing C:/Users/Corey/Dev/statlog/app/views/posts/index.html.erb where line #12 raised: 

undefined method `username' for #<Array:0x3893298> 
Extracted source (around line #12): 

9: <div class="follow-row"> 
10: <div class="titan-users nuvo"><h2>TITAN Users</h2></div> 
11:  <% @users.each do |post| %> 
12:  <div class="user-row"><%= @user.username %></div> 
13:  <% end %> 
14: </div> 
15: 
Rails.root: C:/Users/Corey/Dev/statlog 

Application Trace | Framework Trace | Full Trace 
app/views/posts/index.html.erb:12:in `block in _app_views_posts_index_html_erb___134739565_23193672' 
app/views/posts/index.html.erb:11:in `each' 
app/views/posts/index.html.erb:11:in `_app_views_posts_index_html_erb___134739565_23193672' 
app/controllers/posts_controller.rb:13:in `index' 

楼下是我posts_controller你看见我没有user_controller。

class PostsController < ApplicationController 
    # GET /posts 
    # GET /posts.json 
    def index 
    @posts = Post.all(:order => "created_at DESC") 
    @post = Post.new 

    @users = User.all 


    @user = User.find(:all) 

    respond_to do |format| 
     format.html # index.html.erb 
     format.json { render json: @posts } 
    end 
    end 

    # GET /posts/1 
    # GET /posts/1.json 
    def show 
    redirect_to posts_path 
    end 

    # GET /posts/new 
    # GET /posts/new.json 
    def new 
    @post = Post.new 

    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @post } 
    end 
    end 

    # GET /posts/1/edit 
    def edit 
    @post = Post.find(params[:id]) 
    end 

    # POST /posts 
    # POST /posts.json 
    def create 
    @post = current_user.posts.build(params[:post]) 

    respond_to do |format| 
     if @post.save 
     format.html { redirect_to @post, notice: 'Post was successfully created.' } 
     format.json { render json: @post, status: :created, location: @post } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @post.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PUT /posts/1 
    # PUT /posts/1.json 
    def update 
    @post = Post.find(params[:id]) 

    respond_to do |format| 
     if @post.update_attributes(params[:post]) 
     format.html { redirect_to @post, notice: 'Post was successfully updated.' } 
     format.json { head :no_content } 
     else 
     format.html { render action: "edit" } 
     format.json { render json: @post.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /posts/1 
    # DELETE /posts/1.json 
    def destroy 
    @post = Post.find(params[:id]) 
    @post.destroy 

    respond_to do |format| 
     format.html { redirect_to posts_url } 
     format.json { head :no_content } 
    end 
    end 
end 

这是我的用户和发布模型。

class Post < ActiveRecord::Base 
    attr_accessible :status, :author, :email, :username 

    belongs_to :user 

    validates :status, :presence => true 



end 

我的用户模型

class User < ActiveRecord::Base 
    # Include default devise modules. Others available are: 
    # :token_authenticatable, :confirmable, 
    # :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 

    # Setup accessible (or protected) attributes for your model 
    attr_accessible :email, :password, :password_confirmation, :remember_me, :username 

    has_many :posts 
    # attr_accessible :title, :body 
end 

最后但并非最不重要我的帖子/ index.html.erb视图。

<% if user_signed_in? %> 
    <h1 id="welcome" class="nuvo">Welcome <%= current_user.username %>!</h1> 
<% else %> 
    <h1 id="welcome" class="nuvo">Log-In to make some posts!</h1> 
<% end%> 

<div class="follow-row"> 
    <div class="titan-users nuvo"><h2>TITAN Users</h2></div> 
    <% @users.each do |post| %> 
     <div class="user-row"><%= @user.username %></div> 
    <% end %> 
</div> 

<div class="statuses"> 
    <% if user_signed_in? %><div class="status-form"><%= render 'form' %></div><% end %> 
    <% @posts.each do |post| %> 
    <div class="post"> 
     <div class="tstamp"><strong>Posted <%= time_ago_in_words(post.created_at) %> ago by <%= post.user.username %></strong></div> 
     <div class="status"><%= post.status %></div> 
    </div> 
    <% end %> 
</div> 

我的用户迁移文件

class DeviseCreateUsers < ActiveRecord::Migration 
    def change 
    create_table(:users) do |t| 
     ## Database authenticatable 
     t.string :email,    :null => false, :default => "" 
     t.string :encrypted_password, :null => false, :default => "" 

     ## Recoverable 
     t.string :reset_password_token 
     t.datetime :reset_password_sent_at 

     ## Rememberable 
     t.datetime :remember_created_at 

     ## Trackable 
     t.integer :sign_in_count, :default => 0 
     t.datetime :current_sign_in_at 
     t.datetime :last_sign_in_at 
     t.string :current_sign_in_ip 
     t.string :last_sign_in_ip 

     ## Confirmable 
     # t.string :confirmation_token 
     # t.datetime :confirmed_at 
     # t.datetime :confirmation_sent_at 
     # t.string :unconfirmed_email # Only if using reconfirmable 

     ## Lockable 
     # t.integer :failed_attempts, :default => 0 # Only if lock strategy is :failed_attempts 
     # t.string :unlock_token # Only if unlock strategy is :email or :both 
     # t.datetime :locked_at 

     ## Token authenticatable 
     # t.string :authentication_token 


     t.timestamps 
    end 

    add_index :users, :email,    :unique => true 
    add_index :users, :reset_password_token, :unique => true 
    # add_index :users, :confirmation_token, :unique => true 
    # add_index :users, :unlock_token,   :unique => true 
    # add_index :users, :authentication_token, :unique => true 
    end 
end 

用户名添加到用户迁移文件

class AddUsernameToUser < ActiveRecord::Migration 
    def change 
    add_column :users, :username, :string 
    end 
end 
+0

可以粘贴您的用户迁移的文件?是否有用户名声明? – AdamT 2013-03-17 20:42:46

+0

只是把它放在我的问题的底部 – coreypizzle 2013-03-17 20:45:46

+1

你可以'轨道c'然后键入'用户',然后粘贴响应在这里? – AdamT 2013-03-17 20:56:48

回答

2

你在控制器@user = User.find(:all),所以当你拨打@user.username你会得到你可爱的错误。

也在<% @users.each do |post| %>你有复制粘贴错误(什么是post在这里做?)。渲染时使用本地userRadBrad答案将工作:

<% @users.each do |user| %> 
    <div class="user-row"><%= user.username %></div> 
<% end %> 

@posts.users不会起作用,因为你没有在你的模型关系。

最后清理你的控制器,并删除@user = User.find(:all)

4
<% @users.each do |user| %> 
    <div class="user-row"><%= user.username %></div> 
<% end %> 
+0

这只是吐出错误'未定义的局部变量或方法用户为#<#:0x38a8a30>' – coreypizzle 2013-03-17 20:42:09

0

尝试做这个

<% @posts.users.each do |post| %> 
    <%= user.username %> 
<% end %> 
+0

谢谢,我想这将工作,我会在下次尝试它! – coreypizzle 2013-03-17 21:13:11

0

您可以使用此:

<% @users.each do |post| %> 
    <div class="user-row"><%= post.username %></div> 
<% end %> 

,或者你也可以这样做:

<% @users.each do |user| %> 
    <div class="user-row"><%= user.username %></div> 
<% end %>