在我的 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'
Run Code Online (Sandbox Code Playgroud)
下面是我的posts_controller 你看我没有user_controller。
class PostsController < ApplicationController
# GET /posts
# GET /posts.json …Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我最初创建了一个用户脚手架,然后我创建了一个Devise User模型,只是将属性添加到User模型,但是在我的用户控制器中,每当我尝试使参数"username"成为允许的参数时,它仍然会吐出来错误"用户名不是允许的参数".
我的user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :rememberable, :trackable, :validatable
belongs_to :role
def email_required?
false
end
def email_changed?
false
end
end
Run Code Online (Sandbox Code Playgroud)
我的用户控制器
class UsersController < ApplicationController
before_filter :authenticate_user!
before_action :set_user, only: [:show, :edit, :update, :destroy]
# GET /users
# GET /users.json
def index
@users = User.all
end
# GET /users/1
# GET /users/1.json
def show
end
# GET /users/new
def new
@user = User.new …Run Code Online (Sandbox Code Playgroud) 我正在使用Rails应用程序,在尝试添加"编辑事件"链接后,我一直收到以下错误.
No route matches {:action=>"edit", :controller=>"events"} missing required keys: [:id]
Run Code Online (Sandbox Code Playgroud)
下面是我的index.html.haml
.event_con
%h1 Things you've done
- @events_own.each do |event|
.event_title
= event.activity
.event_rating
%strong Rating :
= event.rating
/ 10
.event_when
%strong Started :
= event.time.to_s(:short)
.event_end
%strong Ended :
= event.timetwo.to_s(:short)
.event_notes
%strong Notes :
= event.notes
= link_to "Edit", edit_event_path
Run Code Online (Sandbox Code Playgroud)
这是我的事件控制器:
class EventsController < ApplicationController
before_action :set_event, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
# GET /events
# GET /events.json
def index
@events = Event.all.order("created_at DESC")
@events_own …Run Code Online (Sandbox Code Playgroud)