我的数据库里有狗表,我想找回来N latest added dogs.
我发现的唯一方法是这样的:
Dogs:all()->where(time, <=, another_time);
Run Code Online (Sandbox Code Playgroud)
还有另外一种方法吗?例如像这样的东西Dogs:latest(5);
非常感谢你的帮助:)
componentWillMount()每次切换路线时,我都是我的人。
还有其他方法可以处理商店状态的变化吗?
当我第一次使用这两个功能时还可以,但是,当我切换路线并返回并再次尝试使用它们时,我收到此消息
warning.js:45 Warning: setState(...): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op. Please check the code for the undefined component.
库存清单.js
import React from "react";
import InventoryItem from "../components/InventoryItem";
import InventoryItemStore from "../stores/InventoryItemStore";
import { Link } from "react-router";
export default class InventoryList extends React.Component {
constructor() {
super();
this.state = {
items: InventoryItemStore.getAll(),
}
}
componentWillMount() {
InventoryItemStore.on("change", () => {
this.setState({ …Run Code Online (Sandbox Code Playgroud) 我有这个验证user:
validates :password,
presence: true,
confirmation: true,
length: { minimum: 6 },
:on => :create
Run Code Online (Sandbox Code Playgroud)
这很明显.当我创建(注册)新的时user,我想填写他们的密码,因此这就是原因presence: true.
案例1)当用户想要更新他的帐户(让我们说改变他的用户名)时,表单只有email和username字段.没关系,验证还可以.
案例2)他忘记了密码,我将他的"忘记密码链接"发送给他,他正在创建新密码的页面上.表单包含以下两个字段:密码和密码确认.但是,他将这两个字段都留空并提交表单.验证通过,因为它是唯一的:on => create!因为案例1)
我无法添加,:on => update因为案例1)不会通过,因为没有密码字段.
在这种情况下我该怎么办?什么是最佳实践或这个"问题"的真正解决方案是什么?
在我的 Laravel 应用程序的本地服务器上,我有 .env 文件。
将更改推送到我使用的服务器
git add .
git commit -m "something"
git push heroku master
但是当我heroku git:clone -a myapp在克隆的应用程序中使用时,没有.env文件......只有.env.development文件。
我做错了什么吗?
我正在尝试使用devise处理用户登录/注册/忘记的密码
我想在我的根URL根目录'home#index'处注册表单
家庭控制器
class HomeController < ApplicationController
include DeviseHelper
def index
end
end
Run Code Online (Sandbox Code Playgroud)
设备助手
module DeviseHelper
def resource_name
:user
end
def resource_class
User
end
def resource
@resource ||= User.new
end
def devise_mapping
@devise_mapping ||= Devise.mappings[:user]
end
end
Run Code Online (Sandbox Code Playgroud)
views / home / index.html.erb
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<div class="field">
<%= f.label :password %>
<% if @minimum_password_length %>
<em>(<%= @minimum_password_length …Run Code Online (Sandbox Code Playgroud) 我正在使用Guzzle来处理外部API.
我这样使用它:
$client = new Client;
$request = $client->request('GET', 'https://api.callrail.com/v1/companies.json', [
'headers' => [
'Authorization' => 'Token token="my_api_string"'
]
]);
return dd($request);
Run Code Online (Sandbox Code Playgroud)
这是输出
Stream {#255 ?
-stream: stream resource @280 ?}
-size: null
-seekable: true
-readable: true
-writable: true
-uri: "php://temp"
-customMetadata: []
}
Run Code Online (Sandbox Code Playgroud)
但是当我像这样使用卷曲时
$api_key = 'my_api_string';
$ch = curl_init($api_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Token token=\"{$api_key}\""));
$json_data = curl_exec($ch);
return dd($json_data);
Run Code Online (Sandbox Code Playgroud)
输出看起来像预期的那样
{"page":1,"per_page":100,"total_pages":1,"total_records":11,
....
....
Run Code Online (Sandbox Code Playgroud)
我对Guzzle做错了什么?
即使通过Ajax提交表单,我也收到422无法处理的实体错误。
我的JavaScript文件
$.ajaxSetup({
headers: {
'X-XSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$('.keywords-plan-form').submit(function(event) {
event.preventDefault();
$.ajax({
url: '/laravel/public/keywordsplans',
type: 'POST',
data: $(this).serialize(),
success: function(data){
alert(data);
// success logic
},
error: function(data){
// Error...
var errors = $.parseJSON(data.responseText);
console.log(errors);
$.each(errors, function(index, value) {
});
}
});
});
Run Code Online (Sandbox Code Playgroud)
如您所见,我在Ajax标头中添加了X-XSRF-TOKEN **** strong文本。
这是我的标签
<meta name="csrf-token" content="{{ csrf_token() }}">
Run Code Online (Sandbox Code Playgroud)
我在Chrome调试器中的表单数据
_token:5j6DGhhTytbIRB1GrW9Wml9XrOxmKjgE9RiGa4Gf
date:
keyword[0]:Lorem ipsum
keyword[1]:Is dolor amet
keyword[2]:plumber tampa
Run Code Online (Sandbox Code Playgroud)
请求标题
X-XSRF-TOKEN:5j6DGhhTytbIRB1GrW9Wml9XrOxmKjgE9RiGa4Gf
.....
Run Code Online (Sandbox Code Playgroud)
我是在做错事还是忘记了事?
是否有可能在laravel中更改字符串,如下所示:
"这是字符串"到"这是字符串 "?
在laravel doc.我只发现了这个:
$ snake = snake_case('fooBar');
// foo_bar
非常感谢您的帮助 :)
我在我的控制器中有这个功能
def create
@tweet = Tweet.new(tweet_params)
@tweet.user_id = current_user.id
respond_to do |format|
if @tweet.save
format.html { redirect_to root_path, notice: "hello" }
format.js { render action: 'create', success: true, location: @tweet }
else
format.html { render action: 'show' }
format.js { render action: 'create', success: false }
end
end
end
Run Code Online (Sandbox Code Playgroud)
表单设置为remote:true所以这是我的create.js.erb
但我得到了他的错误
NoMethodError (undefined method `tweet_url' for #<TweetsController:0x007f8de60e4b58>):
app/controllers/tweets_controller.rb:13:in `block (2 levels) in create'
app/controllers/tweets_controller.rb:9:in `create'
Run Code Online (Sandbox Code Playgroud)
知道为什么会出现这个错误吗?