gpt4 book ai didi

ruby-on-rails - 设计 - 列出用户

转载 作者:行者123 更新时间:2023-12-04 07:30:06 25 4
gpt4 key购买 nike

在我的 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

最后但并非最不重要的是我的 post/index.html.erb View 。

<% 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

最佳答案

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

关于ruby-on-rails - 设计 - 列出用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15465949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com