在我的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 :(得分:4)
<% @users.each do |user| %>
<div class="user-row"><%= user.username %></div>
<% end %>
答案 1 :(得分:2)
您拥有控制器@user = User.find(:all)
,因此当您致电@user.username
时,您会收到错误。
同样在<% @users.each do |post| %>
你有复制粘贴错误(post
在这里做什么?)。渲染时使用本地user
。 RadBrad 答案可行:
<% @users.each do |user| %>
<div class="user-row"><%= user.username %></div>
<% end %>
此@posts.users
无效,因为您的模型中没有这种关系。
最后清理您的控制器并移除@user = User.find(:all)
答案 2 :(得分:0)
尝试这样做
<% @posts.users.each do |post| %>
<%= user.username %>
<% end %>
答案 3 :(得分: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 %>