[英]rails tutorial ch 2 error noMethodError: undefined method 'micropost'
我是新的RoR,并且正在完成以下指导:第2章。
我正在使用通过Sublime Text 2虚拟化的Ubuntu 12.04 LTS。我正在使用Terminal作为命令提示符。
http://ruby.railstutorial.org/ruby-on-rails-tutorial-book
我经历了一些试验和错误,但是现在我陷入了困境。
我正在开发Demo_app,并停留在图2.3.3上,我已经测试了localhost:3000,并且/ users /和/ microposts /页面均已生成,并且所有功能均正常运行。 即新建,编辑,销毁等。
我已经完成了用户和微博耙程序,即“ bundle exec rake db:migrate”,显然是更新了数据模型。
我现在通过在终端上输入'ruby console'进入ruby控制台,完成后我得到1.9.1:001> first_user = User.first,响应是,用户负载(0.1ms)选择“ users”。* FROM“ users” “ LIMIT 1
=>#
我现在得到
1.9.1 :002 > and I type first_user.microposts
这是我通过包含的命令得到的结果错误
1.9.1 :001 > first_user = User.first
User Load (0.1ms) SELECT "users".* FROM "users" LIMIT 1
=> #<User id: 1, name: "User Name", email: "example@example.com", created_at: "2013-06-23 23:17:01", updated_at: "2013-06-23 23:17:01">
1.9.1 :002 > first_user.microposts
NoMethodError: undefined method `microposts' for #<User:0xa6e8afc>
from /home/user/rails_projects/demo_app/vendor/bundle/ruby/1.9.1/gems/activemodel-3.2.13/lib/active_model/attribute_methods.rb:407:in `method_missing'
from /home/user/rails_projects/demo_app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/attribute_methods.rb:149:in `method_missing'
from (irb):2
from /home/user/rails_projects/demo_app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
from /home/user/rails_projects/demo_app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
from /home/user/rails_projects/demo_app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
我想我也会张贴我认为可能与该错误有关的其他文件。
我的micropost.rb
class Micropost < ActiveRecord::Base
attr_accessible :content, :user_id
belongs_to :user
validates :content, :length => { :maximum => 140 }
end
我的users_controller.rb
class UsersController < ApplicationController
# GET /users
# GET /users.json
def index
@users = User.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @users }
end
end
# GET /users/1
# GET /users/1.json
def show
@user = User.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @user }
end
end
# GET /users/new
# GET /users/new.json
def new
@user = User.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @user }
end
end
# GET /users/1/edit
def edit
@user = User.find(params[:id])
end
# POST /users
# POST /users.json
def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
format.html { redirect_to @user, notice: 'User was successfully created.' }
format.json { render json: @user, status: :created, location: @user }
else
format.html { render action: "new" }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
# PUT /users/1
# PUT /users/1.json
def update
@user = User.find(params[:id])
respond_to do |format|
if @user.update_attributes(params[:user])
format.html { redirect_to @user, notice: 'User was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
# DELETE /users/1
# DELETE /users/1.json
def destroy
@user = User.find(params[:id])
@user.destroy
respond_to do |format|
format.html { redirect_to users_url }
format.json { head :no_content }
end
end
end
我的user.rb文件
class User < ActiveRecord::Base
attr_accessible :email, :name
has_many :microposts
end
我的index.html.erb文件
</tr>
<% @users.each do |user| %>
<tr>
<td><%= user.name %></td>
<td><%= user.email %></td>
<td><%= link_to 'Show', user %></td>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New User', new_user_path %>
我的routes.rb
DemoApp::Application.routes.draw do
resources :microposts
resources :users
#...
end
我猜我的Gemfile
source 'https://rubygems.org'
gem 'rails', '3.2.13'
group :development do
gem 'sqlite3', '1.3.5'
end
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '3.2.5'
gem 'coffee-rails', '3.2.2'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', :platforms => :ruby
gem 'uglifier', '1.2.3'
end
gem 'jquery-rails', '2.0.2'
group :production do
gem 'pg', '0.12.2'
end
我所遇到的唯一麻烦是,在阅读本教程时是在2.1上使用命令的地方
捆绑更新
接着
捆绑安装-无生产
为了使更新正常工作,我不得不撤消该过程。我查看了stackoverflow并发现了一些其他解决方法...也许是这种方式或某种方式设置服务器或原始的gem,ruby,rails安装。
感谢您的帮助,如果我没有正确发布此信息,我会提出建议。
Tkin1
您的用户模型看起来像
class User < ActiveRecord::Base
attr_accessible :email, :name
end
没有定义关联。 做了:
class User < ActiveRecord::Base
attr_accessible :email, :name
has_many: :microposts
end
您在帖子中提到了它,但是它并没有反映您的代码
就我而言,问题是我在未保存user.rb
或micropost.rb
情况下启动了rails console
。
我通过退出rails console
,保存两个模型文件并重新启动rails console
解决它。
仅在不重新启动rails console
情况下保存模型文件是不够的。
我遇到了同样的错误,这是修复此错误的方法:输入关联时不要遵循这本书,而是输入
has_many :Micropost
和
belongs_to: User
请注意,“ Micropost”和“ User”均为大写 ,“ Micropost”为单数而不是复数
我确定您现在已经找到答案了,但是我发现我的问题是。
当我这样做时,我没有运行第二个bundle exec rake db:migrate
,它可以修复错误。
这是我为解决问题所做的工作。
1)仔细检查您的micropost.rb和user.rb。
我这里有一个拼写问题,需要纠正。
2)删除旧帖子,创建新帖子
如果您使用旧配置创建记录,则认为会导致问题。 我删除了我的帖子(使用该应用程序),然后为用户1创建了一些新帖子。此后,我能够运行rails console命令。
祝好运!
我不得不通过再次运行rails server
来重新启动Rails miniserver。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.