尝试通过名为"api"的API子域在rails中实现Web服务.
在我的hosts文件中我添加了一行:127.0.0.1 api.localhost
在我routes.rb设置的子域中,我只需要索引操作和少量手动添加的restful路由,通过以下方式:
namespace :api, path: '', :constraints => {:subdomain => "api"} do
resources :posts, only: :index do
collection do
get 'popular_by_day'
get 'popular_by_week'
end
end
end
Run Code Online (Sandbox Code Playgroud)
还生成了相应的控制器: rails g controller api/posts
测试示例:
class Api::PostsController < ApplicationController
def index
@posts = 1
respond_to do |format|
format.json { render :json => @posts }
end
end
def popular_by_day
end
def popular_by_week
end
end
Run Code Online (Sandbox Code Playgroud)
在rake routes我跟随之后:
popular_by_day_api_posts GET /posts/popular_by_day(.:format) api/posts#popular_by_day {:subdomain=>"api"}
popular_by_week_api_posts GET /posts/popular_by_week(.:format) api/posts#popular_by_week …Run Code Online (Sandbox Code Playgroud) 我总是在我的项目中以一个document.ready函数来结束巨大的“script.js”或“main.js”文件,并且总是对此感到难过,但不知道如何自己很好地组织js/jquery代码。我熟悉模块的概念,但又不知道如何去做。
例如,我正在从事一个拥有许多资源(例如组织、用户等)的项目。我将尽力展示我需要对每个资源执行的操作的最少代码:
function deleteResource(url, $that) {
$.ajax({
url: url,
type: "DELETE"
}).done(function() {
$that.closest('tr').remove();
});
}
function emptyHelper(element) {
if(typeof element === "undefined")
return "/";
return element;
}
function createHtml(data) {
var $that = $('.organisations-table tbody');
$that.find("tr").remove();
for(var i=0; i<data.length; i++) {
var element = "<tr>";
element += "<td>" + emptyHelper(data[i].telephone) + "</td>";
element += "<td><a href='#' class='delete-organisation' data-id=" + data[i].organisationId + "></a></td>"
element += "</tr>";
$that.append(element);
}
}
$("tbody").on('click', 'a.delete-organisation', function() {
deleteResource(/*some_url_for_deleting_resource */, $(this));
}); …Run Code Online (Sandbox Code Playgroud) 引用《 RabbitMQ》一书的内容:
当消费者应用程序直接将其已发布的消息直接发送到发布者的所有队列上,或者该消息已排队并在请求时持久存在时,便将Basic.Ack请求发送给发布者。
与混淆Has been directly consumed,是否意味着当消费者发送ack给代理发布者时将被告知消费者处理消息成功?还是意味着当消费者刚刚从队列中收到消息时,发布者将得到通知?
or that the message was enqueued and persisted if requested。这是像变种一样,还是会在发生任何一种情况时通知发布者?(在这种情况下,发布商将收到两次通知)
使用node.js并amqplib想检查实际发生了什么:
// consumer.js
amqp.connect(...)
.then(connection => connection.createChannel())
.then(() => { assert exchange here })
.then(() => { assert queue here })
.then(() => { bind queue and exchange here })
.then(() => {
channel.consume(QUEUE, (message) => {
console.log('Raw RabbitMQ message received', message)
// Simulate some job to do
setTimeout(() => {
channel.ack(message, false)
}, …Run Code Online (Sandbox Code Playgroud) 总之,任何人都可以帮我转换一些数据,这些数据将从模型(基于RAW查询)返回到JSON.
所以在我的控制器中我有类似的东西:
public function get_index() {
$data = Something::getDataFromRawQuery();
return View::make('....')->with('data', $data);
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是如何将JSON数据转发到控制器的视图?
这是查询:
$apps = DB::query('SELECT a.name,
a.desc,
a.sig,
ar.rate
FROM something a
INNER JOIN something_else ar
ON (a.id=ar.something_id)
ORDER BY ar.rate DESC'
);
return $apps;
Run Code Online (Sandbox Code Playgroud) Hy,我对通过迁移在laravel中创建表(通常使用DB)有疑问.
我有类似的东西(来自代码快乐)
<?php
Schema::create('users', function($table) {
$table->increments('id');
$table->string('username', 32);
$table->string('email', 320);
$table->string('password', 64);
$table->integer('role');
$table->boolean('active');
$table->timestamps();
});
Run Code Online (Sandbox Code Playgroud)
好的,这将创建包含9个字段的表"用户",但我对这个回调感到困惑.首先变量"$ table"是哪个类的实例?有人能解释一下这里发生了什么,分别是如何运作的?
为什么嵌套.row占用父.row的高度?
例:
<div class="container">
<div class="row"> <!-- Parent .row -->
<div class="col-md-8">
<div class="col-md-6"></div>
<div class="col-md-3"></div>
<div class="col-md-3"></div>
<div class="row"> <!-- nested PROBLEMATIC ROW -->
<div class="col-md-12">
Why this row has height of 300px instead of 150px?
I can solve problem by setting 'clear: both' on that
row, but class .row should do it by itself.
</div>
</div>
</div>
<div class="col-md-4">
<div></div>
<div></div>
<div></div>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
使用一些虚拟css:
.col-md-8 {
height: 500px;
background: #f0f0f0;
}
.col-md-6 {
height: 150px;
background: …Run Code Online (Sandbox Code Playgroud)