我是一名新的 RoR,正在完成 railstutorial:第 2 章。
我正在使用 Ubuntu 12.04 LTS,使用 Sublime Text 2 进行虚拟化。我使用终端作为命令提示符。
http://ruby.railstutorial.org/ruby-on-rails-tutorial-book
我经历了一些尝试和错误,但现在我被难住了。
我正在开发 Demo_app 并停留在图 2.3.3 我已经测试了我的 localhost:3000 并且 /users/ 和 /microposts/ 页面都已生成,并且一切正常。即新建、编辑、销毁等。
我已经完成了 user 和 micropost rake,“bundle exec rake db:migrate”,显然更新了数据模型。
我现在通过在终端中输入 'ruby console' 进入 ruby 控制台,完成后我得到 1.9.1 :001 > first_user = User.first ,响应是 User Load (0.1ms) SELECT "users".* FROM "users " 限制 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>'
我想我还会发布我认为可能与错误有关的其他文件。
我的微博.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 %>
我的路线.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