在过去的两天左右,我一直在将我的功能转换为mysqli.我遇到了一个问题.我有一个函数返回一个包含数据库中一行的数组.但是,我希望数组包含多行而不是一行.另外,我如何能够回应各个帖子.这是我的失败尝试,只显示数组中的一行.
$mysqli = new mysqli("localhost", "user", "password", "database");
function display_posts ($mysqli, $user_id) {
$fields = "`primary_id`, `poster_id`, `profile_user_id`, `post`";
$user_id = (int)$user_id;
$query = "SELECT DISTINCT $fields FROM `posts` WHERE `profile_user_id` = $user_id
LIMIT 4";
if ($result = $mysqli->query($query)) {
$row = $result->fetch_assoc();
return $row;
$result->free();
$stmt->close();
}}
Run Code Online (Sandbox Code Playgroud)
在这里,我试图显示数据.
$user_id = 1;
$posts = display_posts($mysqli, $user_id);
//Not sure what to do with $posts. A While loop perhaps to display each post?
Run Code Online (Sandbox Code Playgroud) 我运行一个测试的RSpec以确保两种模式之间相互关联has_many和belongs_to.这是我的测试.
describe "testing for has many links" do
before do
@post = Post.new(day: "Day 1", content: "Test")
@link = Link.new(post_id: @post.id, title: "google", url: "google.com")
end
it "in the post model" do
@post.links.first.url.should == "google.com"
end
end
Run Code Online (Sandbox Code Playgroud)
测试告诉我url是一个未定义的方法.我的考试有什么问题?或者我只是错过了一些基本的东西.
Post的模型文件
has_many :links
Run Code Online (Sandbox Code Playgroud)
链接的模型文件
belongs_to :post
Run Code Online (Sandbox Code Playgroud)
最重要的是,链接模型具有属性 post_id
Mongoid中两个模型之间的关系中是否明确要求外键?例如.
class User
include Mongoid::Document
has_many :posts
end
class Post
include Mongoid::Document
belongs_to :user
# Is this necessary below?
field :user_id, type: Integer
end
Run Code Online (Sandbox Code Playgroud)
在讨论关系时,Mongoid网站上的文件并未表明任何字段声明,这就是我要求的原因.
我有两个数组
ordered = [1, 2, 3, 4, 5]
some_list = [2, 6, 4]
Run Code Online (Sandbox Code Playgroud)
我想比较两个数组,然后找到重复项,并将其形成一个新数组.诀窍是按照数组中提供的顺序保持ordered数组.
new_array = [2, 4] # Result should be this
Run Code Online (Sandbox Code Playgroud)
我想过一种方法,但我认为性能可以提高.
ordered.each do |value1|
some_list.include? value1
new_array << value1
end
end
Run Code Online (Sandbox Code Playgroud)
有什么方法可以改善这个吗?
基准测试结果
user system total real
using & 0.210000 0.000000 0.210000 ( 0.212070)
using select 0.220000 0.000000 0.220000 ( 0.218889)
Run Code Online (Sandbox Code Playgroud) 所以我在我看来有一个表格..
form_for(ActivityComment.new, remote: true, url: "/activity_comments/create", class: "col-md-6") do |f|
f.hidden_field :klass_name, value: activity.klass
f.hidden_field :klass_id, value: activity.id
f.text_area :comment, class: "form-control"
f.submit "Submit", class: "btn btn-success"
end
Run Code Online (Sandbox Code Playgroud)
html打印出来......
<form accept-charset="UTF-8" action="/activity_comments/create" class="new_activity_comment" data-remote="true" id="new_activity_comment" method="post">
<div style="margin:0;padding:0;display:inline">
<input name="utf8" type="hidden" value="?">
</div>
<input id="activity_comment_klass_name" name="activity_comment[klass_name]" type="hidden" value="Friend">
<input id="activity_comment_klass_id" name="activity_comment[klass_id]" type="hidden" value="3">
<textarea class="form-control" id="activity_comment_comment" name="activity_comment[comment]"></textarea>
<input class="btn btn-success" name="commit" type="submit" value="Submit">
</form>
Run Code Online (Sandbox Code Playgroud)
然而,我在控制器中收到的参数是......
Parameters: {"utf8"=>"?", "activity_comment"=>{"klass_name"=>"Friend", "klass_id"=>"3", "comment"=>""}, "commit"=>"Submit"}
Run Code Online (Sandbox Code Playgroud)
活动评论模型
# == Schema Information
#
# Table name: …Run Code Online (Sandbox Code Playgroud) 我有一个看起来像这样的订单资源.
.factory('Order', order)
order.$inject = ['$resource', "ApiEndpoint"];
function order($resource, ApiEndpoint) {
return $resource(ApiEndpoint.url + 'orders.json', {}, {
create: {method: 'POST', url: ApiEndpoint.url + 'orders.json'},
update: {method: 'PUT'},
edit: {method: 'GET', url: ApiEndpoint.url + 'orders/edit.json'},
remove_item: {method: 'GET', url: ApiEndpoint.url + 'orders/remove_item.json'},
});
}
Run Code Online (Sandbox Code Playgroud)
我这样跑的Order.update时候
var params = {
order: {
line_items_attributes: {0: {quantity: 2, id: 1}}
},
order_id: 3
};
Order.update(params, function (resp, respHeaders) {
console.log("response headers", respHeaders());
console.log("change quantity resp", resp);
})
Run Code Online (Sandbox Code Playgroud)
我也试过这个:
Order.update({}, params, function (resp, …Run Code Online (Sandbox Code Playgroud) 我的应用程序上有一个Ionic Popover.当我运行离子服务器,离子模拟ios和XCode模拟器时,会出现popover.但是,只要我在XCode上模拟我的iPhone 4S上的应用程序或使用Ionic View应用程序查看我自己的应用程序,就不会出现弹出窗口.我调试了一切,代码不起作用.运行应用程序时,我的控制台上没有错误.
一旦进入蓝色月亮,弹出窗口将出现在我的4S上,但是弹出窗口的出现没有逻辑.我会更改一段代码,弹出窗口,然后当我再次更改它时,弹出窗口消失.我重复这个过程,然后回到我的旧代码版本,它有效,但是不起作用.这令人沮丧.更糟糕的是我担心没有人会回应这个消息.任何关于为什么iPhone模拟器和我的实际iPhone之间存在差异的帮助都会很棒.谢谢.
按钮HTML
<div ng-controller="FilterPopoverController as filterPopover" class="text-right">
<div on-tap="filterPopover.open()" ng-class="{filterButtonOpened: filterPopover.opened}" id="filter-button">
<span class="assertive" >
<i class="icon ion-arrow-down-b"></i>
<span class="bold">FILTER</span>
</span>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
Popover HTML
<ion-popover-view id="filterPopover">
<ion-header-bar class="bar-dark">
<h1 id="popoverTitle" class="bold">FILTER BY</h1>
</ion-header-bar>
<ion-content>
<p>Content here</p>
</ion-content>
</ion-popover-view>
Run Code Online (Sandbox Code Playgroud)
Popover控制器
.controller('FilterPopoverController', filterPopoverController)
filterPopoverController.$inject = ['$ionicPopover', '$filter', '$scope', '$timeout'];
function filterPopoverController($ionicPopover, $filter, $scope, $timeout) {
var vm = this;
vm.open = open;
vm.popover = null;
vm.opened = false;
activate();
//Cleanup the popover when we're done with it! …Run Code Online (Sandbox Code Playgroud) 我正在使用Turbolinks 2.5.3,我在主页的底部有一个脚本标签.
<script>
$(document).ready(function() {
alert('hello');
});
</script>
Run Code Online (Sandbox Code Playgroud)
对于网站上的每个页面,我运行以下JS:
Turbolinks.pagesCached(0);
Run Code Online (Sandbox Code Playgroud)
如果我访问主页,则会出现一次警报.如果我按下后退按钮然后在我的浏览器上转发按钮,主页将加载,现在出现两个警报.如果我再次重复此过程,将出现三个警报.
这是我的问题:
当我明确告诉不要时,为什么Turbolinks会缓存页面.是否与Turbolinks转换文档就绪事件监听器有关?我如何解决这个问题,每次只加载一次?
我有一个Rails 4.2应用程序.几个月前,我们开始遇到无效的真实性令牌错误.我发现以下场景重现了错误:
1)用户打开浏览器并访问ourwebsite.com/log-in.
2)用户在同一浏览器中打开第二个选项卡并访问ourwebsite.com/enroll-in-course.
3)用户返回选项卡1并登录提交POST表单.
4)用户转到选项卡2并在注册课程页面上提交POST表单.
5)出现错误.
以下是有关我们应用的一般信息:
我们正在使用Devise 3.4.1并且应用了零定制.
我们的应用程序控制器运行protect_from_forgery with: :exception.
我们使用Turbolinks 2.5.
我们不会缓存任何一种形式.
我们Turbolinks.pagesCached(0);在所有页面上运行Javascript.
重申一下,上述场景中的两种形式都是POST表单.除上述情况外,它们的工作完全正常.
据我所知,我们在处理用户会话或CSRF令牌时没有做任何更改.
我有一个理论认为,因为我们登录用户,会话中存储的csrf_token会发生变化.因此,当用户在第二个选项卡中提交表单时,表单中的令牌将与会话中的令牌不匹配,并引发InvalidAuthenticityToken.怎么了,怎么解决这个问题?更好的是,这是否可以解决?
我正在为我的网站生成站点地图,并暂时将其保存到tmp文件夹,然后上传到我的Amazon AWS账户.我正在使用站点地图生成器和雾宝石来帮助我.到目前为止我有这个......
# In sitemap.rb
# Set the host name for URL creation
SitemapGenerator::Sitemap.default_host = "http://mycoolapp.com/"
# pick a place safe to write the files
#SitemapGenerator::Sitemap.public_path = 'tmp/'
# store on S3 using Fog
SitemapGenerator::Sitemap.adapter = SitemapGenerator::S3Adapter.new
# inform the map cross-linking where to find the other maps
SitemapGenerator::Sitemap.sitemaps_host = "http://#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com/"
# pick a namespace within your bucket to organize your maps
SitemapGenerator::Sitemap.sitemaps_path = '/'
SitemapGenerator::Sitemap.create do
# Put links creation logic here.
#
add '/home'
add '/about'
add '/contact' …Run Code Online (Sandbox Code Playgroud)