我是Rails的新手,目前正在通过指南.该指南指出:
使用文本编辑器使用清单2.1的内容更新Bundler所需的Gemfile.
Run Code Online (Sandbox Code Playgroud)source 'https://rubygems.org' gem 'rails', '3.2.3' group :development do gem 'sqlite3', '1.3.5' end # Gems used only for assets and not required # in production environments by default. group :assets do gem 'sass-rails', '3.2.4' gem 'coffee-rails', '3.2.2' gem 'uglifier', '1.2.3' end gem 'jquery-rails', '2.0.0' group :production do gem 'pg', '0.12.2' end
然后,我们使用以下
bundle install
命令安装并包含gem :Run Code Online (Sandbox Code Playgroud)$ bundle install --without production
如果Bundler抱怨
no such file to load -- readline (LoadError)
尝试添加gem ’rb-readline’
到您的Gemfile.)
我按照步骤甚至添加gem 'rb-readline'
到了Gemfile …
每次我想用jQuery向Jails发布一个JSON对象数组时,我就遇到了这个问题.如果我对数组进行字符串化,我可以看到jQuery正在正常工作:
"shared_items"=>"[{\"entity_id\":\"253\",\"position\":1},{\"entity_id\":\"823\",\"position\":2}]"
Run Code Online (Sandbox Code Playgroud)
但是如果我只是将它作为AJAX调用的数据发送给我:
"shared_items"=>{"0"=>{"entity_id"=>"253", "position"=>"1"}, "1"=>{"entity_id"=>"823", "position"=>"2"}}
Run Code Online (Sandbox Code Playgroud)
然而,如果我只发送一个普通数组,它的工作原理:
"shared_items"=>["entity_253"]
Run Code Online (Sandbox Code Playgroud)
为什么Rails将数组更改为那个奇怪的哈希?想到的唯一原因是Rails无法正确理解内容,因为这里没有类型(有没有办法在jQuery调用中设置它?):
Processing by SharedListsController#create as
Run Code Online (Sandbox Code Playgroud)
谢谢!
更新:
我将数据作为数组发送,而不是字符串,并使用该.push()
函数动态创建数组.尝试$.post
和$.ajax
,相同的结果.
使用成语时如:
def func(*args)
# some code
end
Run Code Online (Sandbox Code Playgroud)
是什么意思*args
?谷歌搜索这个具体问题非常困难,我找不到任何东西.
似乎所有的论点都出现了,args[0]
所以我发现自己编写的防御性代码如下:
my_var = args[0].delete(:var_name) if args[0]
Run Code Online (Sandbox Code Playgroud)
但我确信我错过了更好的方式.
我正试图psql
在我的Vagrant机器上运行,但是我收到了这个错误:
psql: could not connect to server: No such file or directory
Is the server running locally and accepting connections on
Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
Run Code Online (Sandbox Code Playgroud)
注意:Vagrant 1.9.2 Box:ubuntu/trusty64,https://atlas.hashicorp.com/ubuntu/boxes/trusty64
编辑 命令我用来安装和运行postgres:
sudo apt-get update
sudo apt-get install postgresql
sudo su postgres
psql -d postgres -U postgres
我是一名PHP开发人员,通过阅读Michael Hartl的教程来学习Ruby on Rails .以下是本书的引用,指的是csrf_meta_tag
:
... Rails方法
csrf_meta_tag
[防止]跨站点请求伪造(CSRF),一种恶意Web攻击.不要担心细节(我没有); 只知道Rails正在努力保证您的应用程序的安全.
问题是,我真的很好奇.插入csrf-param
和csrf-token
元标记如何阻止CSRF?我试过谷歌搜索,但找不到任何东西.
1)宝石里面有耙子任务的"最佳"位置吗?我见过它们/tasks
,/lib/tasks
我看到它们写成了- *.rb
并且*.rake
不确定哪个(如果有的话)是'正确的'
2)在环境中配置gem后,如何将它们提供给应用程序?
我希望将原始发布数据(例如,未配置的JSON)发送到我的一个控制器进行测试:
class LegacyOrderUpdateControllerTest < ActionController::TestCase
test "sending json" do
post :index, '{"foo":"bar", "bool":true}'
end
end
Run Code Online (Sandbox Code Playgroud)
但这给了我一个NoMethodError: undefined method `symbolize_keys' for #<String:0x00000102cb6080>
错误.
发送原始帖子数据的正确方法是什么ActionController::TestCase
?
这是一些控制器代码:
def index
post_data = request.body.read
req = JSON.parse(post_data)
end
Run Code Online (Sandbox Code Playgroud) 我正在尝试编译这样的C++程序:
$ g++ -o Sniffer_Train main.cpp Sniffer_train.cpp Sniffer_train.h -lmysqlclient -lpcap
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
/usr/bin/ld: /tmp/cct6xeXD.o: undefined reference to symbol
'pthread_join@@GLIBC_2.4' //lib/arm-linux-gnueabihf/libpthread.so.0:
error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
我不知道这个错误意味着什么.任何帮助将不胜感激.
我试图在Rails中使用ActiveRecord进行查询,该查询指定了连接表上的某些条件.我似乎无法让它工作,即使我按照这里的例子:
http://guides.rubyonrails.org/active_record_querying.html#specifying-conditions-on-the-joined-tables
从指南:
Client.joins(:orders).where(:orders => {:created_at => time_range})
我的数据库架构是这样的,有桌子scores
,submissions
和tasks
:
create_table "scores", :force => true do |t|
t.integer "value"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "scores", ["user_id"], :name => "index_scores_on_user_id"
create_table "submissions", :force => true do |t|
t.integer "user_id"
t.integer "task_id"
t.integer "score_id"
t.datetime "completed_at"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "submissions", ["score_id"], :name => "index_submissions_on_score_id"
add_index "submissions", ["task_id"], :name => "index_submissions_on_task_id"
add_index "submissions", ["user_id"], :name => "index_submissions_on_user_id"
create_table "tasks", :force …
Run Code Online (Sandbox Code Playgroud) 我正在使用Rails,Puma,Capistrano3.我也安装了宝石capistrano3-puma
.我和Puma Jungle一起创办了Puma https://github.com/puma/puma/tree/master/tools/jungle/upstart
如何在部署期间重新启动Puma?