我确信这很简单,所以对这个天真的问题道歉.
我正在尝试测试我当前的页面是否正确'jobs#index'.如果它是我有一些额外的CSS和内容,我将包括在页面中,如果没有,那么我希望它加载页面没有这个额外的内容.
我相信它应该是这些方面的东西,但我似乎无法做到正确.
<% if path == 'jobs#index' %>
<div class="slidingDiv">
Fill this space with really interesting content.
</div>
<div class="show_hide">
FILTERS
</div>
<% else %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
任何建议真的非常感谢!
我正在尝试编写一个基本功能,允许我为删除空格的英国邮政编码添加空格.
英国邮政编码在邮政编码字符串的最后一位数字之前始终有一个空格.
一些没有间距和正确间距的例子:
CB30QB => CB3 0QB
N12NL => N1 2NL
OX144FB => OX14 4FB
Run Code Online (Sandbox Code Playgroud)
要找到字符串中的最后一位数,我是正则表达式/\d(?=\D*$)/g,我现有的Javascript如下:
// Set the Postcode
var postCode = "OX144FB";
// Find the index position of the final digit in the string (in this case '4')
var postcodeIndex = postCode.indexOf(postCode.match(/\d(?=\D*$)/g));
// Slice the final postcode at the index point, add a space and join back together.
var finalPostcode = [postCode.slice(0, postcodeIndex), ' ', postCode.slice(postcodeIndex)].join('');
return finalPostcode;
Run Code Online (Sandbox Code Playgroud)
当我更改set postcost时,我得到以下结果:
CB30QB becomes CB3 0QB - Correct …Run Code Online (Sandbox Code Playgroud) 我有一些URL都遵循相同的结构.
https://www.website.com/services/county/town/servicename/brand/
当搜索结果为零时,我们会显示一个按钮,当单击该按钮时会运行一个函数来删除URL的最后一部分,从而扩展搜索范围.
例如,如果上面的URL返回0结果,则单击我们的按钮将加载https://www.website.com/services/county/town/servicename/已从brand搜索条件中删除并扩大结果的可能性.
我目前的代码有用,但看起来有点像黑客.
function expandSearch() {
var currentURL = window.location.href;
var parts = currentURL.split("/");
var lastPart;
if ( parts.length === 9 ) {
lastPart = currentURL.substr(currentURL.lastIndexOf('/') - parts[7].length) + '$';
window.location.href = currentURL.replace( new RegExp(lastPart), "");
} else if ( parts.length === 8 ) {
lastPart = currentURL.substr(currentURL.lastIndexOf('/') - parts[6].length) + '$';
window.location.href = currentURL.replace( new RegExp(lastPart), "");
} else if ( parts.length === 7 ) {
lastPart = currentURL.substr(currentURL.lastIndexOf('/') - parts[5].length) + '$'; …Run Code Online (Sandbox Code Playgroud) 好吧,我已经在这个工作了大约2个小时,我相信有一个非常简单的解决方案但不幸的是作为一个铁路新手,我似乎无法找到它.
我按照Ryan Bates认证课程#270建立了一个基本的注册和登录系统.
但是我在尝试登录时仍然收到同样的错误.
undefined method `find_by_email' for nil:NilClass
Run Code Online (Sandbox Code Playgroud)
我的会话控制器设置如下:
class SessionsController < ApplicationController
def new
end
def create
user = user.find_by_email(params[:email])
if user && user.authenticate(params[:password])
session[:user_id] = user.id
redirect_to root_url, :notice => "Logged in!"
else
flash.now.alert = "Invalid email or password"
render "new"
end
end
def destroy
session[:user_id] = nil
redirect_to root_url, :notice => "Logged Out!"
end
end
Run Code Online (Sandbox Code Playgroud)
但我真的不确定如何定义find_by_email.
人们可以提供给我的任何建议真的非常感谢
我有一个非常简单的应用程序,我试图从子组件更新父组件的状态,如下所示:
import React from '../../../../../../../dependencies/node_modules/react';
import ReactDOM from '../../../../../../../dependencies/node_modules/react-dom';
class CalendarMain extends React.Component {
constructor() {
super();
}
handleClick() {
this.props.handleStateClick("State Changed");
}
render() {
return (
<div>
<div className="calendar">
{this.props.checkIn}
<button onClick={ this.handleClick.bind(this) }>Click Me</button>
</div>
</div>
)
}
}
class CalendarApp extends React.Component {
constructor() {
super();
this.state = {
checkIn: "Check-in",
checkOut: "Check-out",
dateSelected: false
};
}
handleStateClick( newState ) {
this.setState({
checkIn: newState
});
}
render() {
return (
<div>
<CalendarMain
checkIn = { this.state.checkIn } …Run Code Online (Sandbox Code Playgroud) 我正在使用 reactJS 应用程序并具有以下代码:
renderProductBlock(product, index) {
return (
<div className="product col-xs-4">
<span className="product-name">{product.name}</span>
<span className="product-price">{product.price}</span>
<a href="#" className="btn btn-primary">Buy Now</a>
</div>
);
}
renderProductList() {
let blocks = [];
_.forEach(product, (item, index) => {
const productBlock = this.renderProductBlock(item, index);
if(productBlock) {
blocks.push(productBlock);
}
});
return blocks;
}
render() {
const productsBlock = this.renderProductList();
if(productsBlock) {
return (
<div className="products">
<div className="row">
{productsBlock}
</div>
</div>
)
}
}
Run Code Online (Sandbox Code Playgroud)
在此布局中输出 HTML:
<div class="products">
<div class="row">
<div class="product col-xs-4">
Content
</div>
<div class="product …Run Code Online (Sandbox Code Playgroud) 我有一个应用程序,有两个型号类别和产品
产品属于类别.
我创建了一个导航栏下拉列表,要求@category和@product对象在整个应用程序中可用(导航栏显示在应用程序的每个页面上.)
我无法解决的问题是放置这些基本定义的最佳位置,而不是在每个页面定义中多次定义它们.
我需要定义以下内容:
@category = Category.all
@products = @category.products.all
Run Code Online (Sandbox Code Playgroud)
然后导航栏循环看起来像这样.
<% @category.each do |c| %>
<%= c.name %>
<% @products.each do |p| %>
<% link_to product_path(p) do %>
<%= p.name %>
<% end %>
<% end %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
我是一个铁杆新手,所以我相信这里有一些错误,但任何帮助将不胜感激!
javascript ×4
if-statement ×2
jquery ×2
reactjs ×2
ruby ×2
ecmascript-6 ×1
function ×1
indexof ×1
loops ×1
object ×1
regex ×1
slice ×1
state ×1