谢谢,这些方法看起来是我想要的。
另外问一下 是不是 jquery file upload 必须要传到在线服务器的?本地不可以用的吗?
还有问题,carrierwave 只能传一个图片 (不过弄完之后我发现这个确实很方便), 现在我想可以传重复的图片。
我刚才试了 jQuery File Upload 的 Basic plugin (minimal setup guide) 和 Rails + (Carrierwave || Dragonfly) Rails-setup-for-V6 (user contribution),但是都没有成功。
还有看到支持 rails 的 tutorial 都要 amazon s3(我没有美元信用卡申请不了),还有看到一个 google app engine 的,小试了一下,怀疑得配置更多东西,懒得弄了。
不知道上传多张图片得还有什么好得 gem,专门正对 rails 的。今天进度缓慢,只搞了这个 carrierwave!
#22 楼 @HungYuHei 呵呵
rake db:schema:load
我运行这条命令,所有数据库内容都被清空了,啊!!(如果你刚好 google 进来看到这个,请谨慎!)
actionpack (3.2.6) lib/action_controller/metal/hide_actions.rb:36:in `visible_action?'
actionpack (3.2.6) lib/action_controller/metal/hide_actions.rb:18:in `method_for_action'
actionpack (3.2.6) lib/action_controller/metal/implicit_render.rb:14:in `method_for_action'
actionpack (3.2.6) lib/action_controller/metal/compatibility.rb:61:in `method_for_action'
actionpack (3.2.6) lib/abstract_controller/base.rb:115:in `process'
actionpack (3.2.6) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.6) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.6) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.6) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.6) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.6) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.6) lib/action_dispatch/routing/route_set.rb:36:in `call'
journey (1.0.4) lib/journey/router.rb:68:in `block in call'
journey (1.0.4) lib/journey/router.rb:56:in `each'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.6) lib/action_dispatch/routing/route_set.rb:600:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.1) lib/rack/etag.rb:23:in `call'
rack (1.4.1) lib/rack/conditionalget.rb:25:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.1) lib/rack/session/abstract/id.rb:205:in `context'
rack (1.4.1) lib/rack/session/abstract/id.rb:200:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/cookies.rb:338:in `call'
activerecord (3.2.6) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:473:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.6) lib/active_support/callbacks.rb:405:in `_run__1603190885298670096__call__4510840069410552607__callbacks'
activesupport (3.2.6) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.6) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.6) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.6) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.6) lib/rails/rack/logger.rb:26:in `call_app'
railties (3.2.6) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.1) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.6) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.1) lib/rack/lock.rb:15:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.6) lib/rails/engine.rb:479:in `call'
railties (3.2.6) lib/rails/application.rb:220:in `call'
rack (1.4.1) lib/rack/content_length.rb:14:in `call'
railties (3.2.6) lib/rails/rack/log_tailer.rb:17:in `call'
rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
/Users/yuwen/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/Users/yuwen/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/Users/yuwen/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
#10 楼 @Rei 是在 work_controller 里面?
class WorksController < ApplicationController
# GET /works
# GET /works.json
def index
@works = Work.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @works }
end
end
# GET /works/1
# GET /works/1.json
def show
@work = Work.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @work }
end
end
# GET /works/new
# GET /works/new.json
def new
@work = Work.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @work }
end
end
# GET /works/1/edit
def edit
@work = Work.find(params[:id])
end
# POST /works
# POST /works.json
def create
@work = Work.new(params[:work])
respond_to do |format|
if @work.save
format.html { redirect_to @work, notice: 'Work was successfully created.' }
format.json { render json: @work, status: :created, location: @work }
else
format.html { render action: "new" }
format.json { render json: @work.errors, status: :unprocessable_entity }
end
end
end
# PUT /works/1
# PUT /works/1.json
def update
@work = Work.find(params[:id])
respond_to do |format|
if @work.update_attributes(params[:work])
format.html { redirect_to @work, notice: 'Work was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @work.errors, status: :unprocessable_entity }
end
end
end
# DELETE /works/1
# DELETE /works/1.json
def destroy
@work = Work.find(params[:id])
@work.destroy
respond_to do |format|
format.html { redirect_to works_url }
format.json { head :no_content }
end
end
end
shell 里面:
➜ ctd git:(master) ✗ rails g uploader image
create app/uploaders/image_uploader.rb
➜ ctd git:(master) ✗ rails g scaffold Work title:string description:text
invoke active_record
create db/migrate/20120725114714_create_works.rb
create app/models/work.rb
invoke test_unit
create test/unit/work_test.rb
create test/fixtures/works.yml
invoke resource_route
route resources :works
invoke scaffold_controller
create app/controllers/works_controller.rb
invoke erb
create app/views/works
create app/views/works/index.html.erb
create app/views/works/edit.html.erb
create app/views/works/show.html.erb
create app/views/works/new.html.erb
create app/views/works/_form.html.erb
invoke test_unit
create test/functional/works_controller_test.rb
invoke helper
create app/helpers/works_helper.rb
invoke test_unit
create test/unit/helpers/works_helper_test.rb
invoke assets
invoke coffee
create app/assets/javascripts/works.js.coffee
invoke scss
create app/assets/stylesheets/works.css.scss
invoke scss
conflict app/assets/stylesheets/scaffolds.css.scss
Overwrite /Users/yuwen/Documents/bitbucket/ctd/app/assets/stylesheets/scaffolds.css.scss? (enter "h" for help) [Ynaqdh] n
skip app/assets/stylesheets/scaffolds.css.scss
➜ ctd git:(master) ✗ rake db:migrate
== CreateWorks: migrating ====================================================
-- create_table(:works)
-> 0.0015s
== CreateWorks: migrated (0.0016s) ===========================================
➜ ctd git:(master) ✗ rails g migration add_image_to_works image:string
invoke active_record
create db/migrate/20120725115122_add_image_to_works.rb
➜ ctd git:(master) ✗ rake db:migrate
== AddImageToWorks: migrating ================================================
-- add_column(:works, :image, :string)
-> 0.0399s
== AddImageToWorks: migrated (0.0400s) =======================================
Gemfile:
source 'https://rubygems.org'
gem 'rails', '3.2.6'
gem 'sqlite3'
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
# gem 'jbuilder'
# gem 'debugger'
gem 'carrierwave'
# gem 'paperclip',:git => 'git://github.com/thoughtbot/paperclip.git'
我看到教程是只有加了这些它那里就可以上传了,我这儿报错。
class Work < ActiveRecord::Base
attr_accessible :description, :title, :image
mount_uploader :image, ImageUploader
end
是不是这里?
报错:
NoMethodError in WorksController#new
undefined method `key?' for nil:NilClass
Rails.root: /Users/yuwen/Documents/bitbucket/ctd
Application Trace | Framework Trace | Full Trace
Request
Parameters:
None
Show session dump
Show env dump
Response
Headers:
None
db/migrate/20120725115122_add_image_to_works.rb
class AddImageToWorks < ActiveRecord::Migration
def change
add_column :works, :image, :string
end
end
db/migrate/20120725114714_create_works.rb
class CreateWorks < ActiveRecord::Migration
def change
create_table :works do |t|
t.string :title
t.text :description
t.timestamps
end
end
end
http://0.0.0.0:3000/works/new:也是返回
NoMethodError in WorksController#new
undefined method `key?' for nil:NilClass
是我在 works controller 里面少什么?但是教程里面没有提到
这个 carriewave 哪里出错了? bundl show:
...
* carrierwave (0.6.2)
...
Gemfile:
...
gem 'carrierwave'
...
work.rb
class Work < ActiveRecord::Base
attr_accessible :description, :title, :image
mount_uploader :image, ImageUploader
end
works/_form.html.erb
<%= form_for(@work, :html => {:multipart => true}) do |f| %>
# rest field
<div class="field">
<%= f.label :image %><br />
<%= f.file_field :image %>
</div>
# simbit
<% end %>
打开http://0.0.0.0:3000/works,:返回
NoMethodError in WorksController#index
undefined method `key?' for nil:NilClass
这是我完全参照视频教程来做的,不知道哪里出错了
很好,估计女孩子看了都会爱上你吗?太有才了
辛苦了,感谢大力贡献!
待会看,谢谢!
我在想,如果也把{}去掉,通过缩进来写,是不是更好看一些?
谢谢 先收下了
简易安装和运行 mongodb(安装由 homebrew 直接安装):
brew install mongodb
只要运行这条命令就可直接安装完成。
建立 mongodb 文件夹:
sudo mkdir -p /data/db/
接着:
sudo chown `id -u` /data/db #这条命令是什么意思?
现在,再次在 shell 里面输入:
mongod
就可以运行 mongo 数据库了。
#2 楼 @fresh_fish 谢谢可以了。
rails 默认 id 排序:1,2,3,4..这样排列,结果就是最先发表的 id 为 1 排在最前。 只要在创建的新闻模型中加上以下代码即可将结果按照符合人的逻辑排列:
default_scope order: 'id desc'
完整的是这样的: 找到文件:project/app/models/article.rb,在开头加入即可
class Article < ActiveRecord::Base
default_scope order: 'id desc' #注意是'id desc'
#...
end