我正在尝试访问我的rails应用程序中的资源,我希望根据URL中的变量填充信息.鉴于URL:
someapp.com/resources/variable/resource
Run Code Online (Sandbox Code Playgroud)
我想传递variable
给Rails来添加元素resource
.是否有任何快速重新路由方式来实现这一目标?还是它太疯狂了?
干杯!
假设我new
在控制器中有一个标准方法:
def new
@doc = Doc.new
respond_to do |format|
format.html
format.json { render json: @doc }
end
end
Run Code Online (Sandbox Code Playgroud)
我如何通过它来促进传递论证,即:
def new(i)
...
end
Run Code Online (Sandbox Code Playgroud)
允许我在视图中写这样的东西:
<%= link_to(e.name, new_doc_path(e.id)) %>
Run Code Online (Sandbox Code Playgroud)
干杯!
我有一个getJSON
脚本应该响应,如果Ruby值不是nil
,但我不知道如何if not (ruby's version of) nil
通过JavaScript 检查.到目前为止它看起来像这样:
$.getJSON("<%= xhr_document_path(document) %>", function(d) {
if (d.ok) {
$('#modal_<%= document.id %>').modal('show');
clearInterval(doc_modal_<%= document.id %>);
} else if (d.error != '') {
window.location.href = "<%= error_document_path(document) %>";
}
});
Run Code Online (Sandbox Code Playgroud)
但是else if
当值为零时,条件会被触发.我如何not nil
在这里查询?
我有一个类可以建立这样的关系:
def assign_vars
template_variables.each do |master|
@document.template_variables.find_or_initialize_by(
name: master.name, tag: master.tag, text: master.default_value)
end
end
end
Run Code Online (Sandbox Code Playgroud)
这样如果它已经存在就会被发现,或者如果它不存在就会被构建。我的问题是text: master.default_value
. 我想仅在我们建立新关系时才设置它,而不是在find_or_initialize_by
. 类似的东西text: text || master.default_value
。我怎么能在那个循环中写呢?
我在我的Phoenix/React应用上的Chrome控制台中收到此错误 - 但奇怪的是,并非处于隐身模式.一切都指向同一个/socket
端点.
WebSocket connection to 'ws://localhost:4000/socket/websocket?token=blah&vsn=1.0.0' failed: Error during WebSocket handshake: Unexpected response code: 400
Run Code Online (Sandbox Code Playgroud)
各种消息来源说它是关于传输层的,这有点意义,因为这是在控制台中突出显示的错误是:
this.conn = new this.transport(this.endPointURL());
Run Code Online (Sandbox Code Playgroud)
React应用程序像这样找到端点:
const API_URL = 'http://localhost:4000/api';
const WEBSOCKET_URL = API_URL.replace(/(https|http)/, 'ws').replace('/api', '');
function connectToSocket(dispatch) {
const token = JSON.parse(localStorage.getItem('token'));
const socket = new Socket(`${WEBSOCKET_URL}/socket`, {
params: { token },
});
socket.connect();
dispatch({ type: 'SOCKET_CONNECTED', socket });
}
Run Code Online (Sandbox Code Playgroud)
注意:修改该行const socket = new Socket('${WEBSOCKET_URL}/socket/websocket'
只会使错误被读取.../socket/websocket/websocket?...
.
这endpoint.ex
很标准:
socket "/socket", App.UserSocket
Run Code Online (Sandbox Code Playgroud)
然而,这些都没有说明为什么应用程序会隐姓埋名并加载所有这些错误.
假设我有这样的范围:
scope :by_templates, ->(t) { joins(:template).where('templates.label ~* ?', t) }
Run Code Online (Sandbox Code Playgroud)
如何检索多个模板t
?
Document.first.by_templates(%w[email facebook])
Run Code Online (Sandbox Code Playgroud)
此代码返回此错误.
PG::DatatypeMismatch: ERROR: argument of AND must be type boolean, not type record
LINE 1: ...template_id" WHERE "documents"."user_id" = $1 AND (templates...
Run Code Online (Sandbox Code Playgroud) 如何在保持其长度为at的同时确保此数组的唯一性5
?
def fixed
5.times.collect { SecureRandom.random_number(10) }
end
Run Code Online (Sandbox Code Playgroud)
这种行为似乎很奇怪:
5.times.collect.uniq { SecureRandom.random_number(10) }
# => [0, 2, 3, 4]
5.times.collect.uniq { SecureRandom.random_number(10) }
# => [0, 1, 3]
5.times.collect.uniq { SecureRandom.random_number(10) }
# => [0, 1, 2, 3, 4]
5.times.collect.uniq { SecureRandom.random_number(10) }
# => [0, 1, 2, 4]
5.times.collect.uniq { SecureRandom.random_number(10) }
# => [0, 1, 2, 3]
Run Code Online (Sandbox Code Playgroud) 据我所知,这段代码应该可行,但事实并非如此,而且我完全难过了.有什么想法吗?
jQuery的
$('.article_chooser').click(function() {
var src = $(this).attr('value');
$("#doc_edits_attributes_0_body").parent("iframe html body").html($("#article"+src).html());
console.log($("#doc_edits_attributes_0_body").parent("iframe html body").html());
});
Run Code Online (Sandbox Code Playgroud)
HTML
<textarea class="editable_areas" cols="40" id="doc_edits_attributes_0_body" name="doc[edits_attributes][0][body]" rows="20" style="display: none;"></textarea>
<iframe class="wysihtml5-sandbox" security="restricted" allowtransparency="true" frameborder="0" width="0" height="0" marginwidth="0" marginheight="0">
<html>
<body marginwidth="0" marginheight="0" contenteditable="true" class="editable_areas wysihtml5-editor" spellcheck="true">
</body>
</html>
</iframe>
Run Code Online (Sandbox Code Playgroud)
干杯!
我有这个方法:
def progress_percentage
(progress_index / positive_statuses.size) * 100
end
Run Code Online (Sandbox Code Playgroud)
它正在回归0
,它应该是38.46
.
@lead.progress_index # this is 5
@lead.positive_statuses.size # this is 13
@lead.progress_percentage # this is 0?
Run Code Online (Sandbox Code Playgroud)
前两个值已得到确认.到底是怎么回事?编辑:在此视图中确认.
我在页面中有这个Javascript:
<script type='text/javascript'>
function update_price(order_id, quantity) {
$.ajax({
beforeSend: function(xhr) {
xhr.setRequestHeader(
'X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'));
},
url: "/carts/" + <%= @cart.id %> + "/update_quantity", #you are missing 's' here is this a typo?
type: "POST",
data: {
"order_id" : order_id,
"quantity" : quantity }
});
}
</script>
Run Code Online (Sandbox Code Playgroud)
它适用于用户.匹配的路由如下所示:
resources :carts, only: %i[update destroy index] do
member do
match 'update_quantity', to: 'carts#update_quantity', via: %i[get post]
end
end
Run Code Online (Sandbox Code Playgroud)
我的规格看起来像这样:
it 'updates the quantity' do
post "/cart/#{cart.id}/update_quantity", xhr: true, params: {
order_id: cart.orders.first.id,
quantity: …
Run Code Online (Sandbox Code Playgroud) 这是编写可以容纳多个参数查询的索引方法的最佳方法吗?
if params.key?(:user_id)
Post.where(user_id: params[:user_id])
elsif params.key?(:status)
Post.where(status: params[:status])
elsif params.key?(:user_id) && params.key?(:status)
Post.where(user_id: params[:user_id], status: params[:status])
else
Post.all
end
Run Code Online (Sandbox Code Playgroud)
当有两个时,我的方法似乎没有抓住params.
我正在编写自定义 OAuth2 策略。我需要向expired?
哈希响应添加一个方法,但我不知道该怎么做。我得到的响应给了我一个时间直到到期,但不是过期的方法本身。
我需要添加一个过期的?方法,不知道如何。有什么帮助吗?