我有一个"名称"数组,我想显示当前用户的名称,只要它是数组中唯一的名称.如果数组中还有其他名称,我不想显示当前用户的名称.
目前,我列出了名称并排除了当前用户的名称,但如果它是数组中唯一的名称,则不希望排除当前用户的名称.我希望我解释说没关系.
我的代码现在:
module ConversationsHelper
def other_user_names(conversation)
users = []
conversation.messages.map(&:receipts).each do |receipts|
users << receipts.select do |receipt|
receipt.receiver != current_user
end.map(&:receiver)
end
users.flatten.uniq.map(&:name).join ', '
end
end
Run Code Online (Sandbox Code Playgroud) 我正在创建rails API.我有一个预定义值的模型
class CreateMessages < ActiveRecord::Migration
def change
create_table :messages do |t|
t.string :status #set [delivered, pending, error]
end
end
end
Run Code Online (Sandbox Code Playgroud)
属性状态可能只有3个值delivered, pending, error.如何在模型中添加字段状态验证
我遇到了以下代码行:
self.update(next_question_at: (next_question_at.next_week).change(hour: 10))
Run Code Online (Sandbox Code Playgroud)
我正在谷歌搜索该change方法但无法找到它的文档,该方法做什么?
我有一个显示页面,列出了我的数据库中的所有对象.我有一个删除链接,将该对象的ID发送到关联控制器中的destroy方法,然后我可以轻松删除该对象并重定向回显示页面.
在同一个展示页面上,我在每个相应的对象旁边都有一组内嵌图像.每个图像实际上都是一个链接(首先除了不应该删除).当我单击特定图像时,我将对象的ID加上对象的列名作为字符串发送到名为destroyImage的新自定义方法.
我在我的控制器中创建了这个方法:
def destroyImage
garment = Parse::Query.new("Garments").eq("objectId", params[:id]).get.first
garment[params[:imageToDelete]] = nil
if garment.parse_delete
flash[:success] = "Image successfully deleted!"
redirect_to adminpanel_path
else
flash[:error] = "Image not deleted, try again!"
render "show"
end
end
Run Code Online (Sandbox Code Playgroud)
在我看来:
<td class= "images">
<div id="deleteText"><%= "Click on image to delete." %></div>
<%= image_tag garment["image"].url if garment["image"] %>
<%= link_to image_tag(garment["image2"].url), destroyImage_adminpanel_path(:id => garment["objectId"], :imageToDelete => "image2"), :method => 'delete' if garment["image2"] %>
<%= link_to image_tag(garment["image3"].url), destroyImage_adminpanel_path(:id => garment["objectId"], :imageToDelete => "image3"), :method => 'delete' if garment["image3"] …Run Code Online (Sandbox Code Playgroud) 我的数据库上有一个名为的表budgets.它有author_id,status,answered_at列.
如果status != 1,那么author_id和answered_at应nil/ null.
我有以下方法budgets_controller.rb:
def update
budget = Budget.find(params[:id])
budget.update_attributes(status: 1, author_id: current_user.id, answered_at: DateTime.now.to_date)
budget.save!
end
Run Code Online (Sandbox Code Playgroud)
我想知道是否可以重复使用相同的方法(update)来改变author_id和answered_at为null,status本身0如果是已 1.某种toggle.
我看到Rails提供了这种方法来切换布尔值,但我无法看到它如何能够满足我的需要,因为我正在使用另外两个不是布尔值的列.
我是rails的新手,我刚在我的应用程序中设置了一个博客.
我希望我的客人能够查看/查看博客,但无法进行任何更改.更改编辑/更新功能仅适用于管理员.
我目前有cancancan和devise安装,他们工作得很好.
我的vblogs/index.html.erb文件目前看起来像这样:
<% @vblogs.each do |vblog| %>
<h2><%= vblog.title %></h2>
<hr>
<br>
<p><b>Created By:<%= vblog.author %></b></p>
<br>
<p><b>Posted On:<%= vblog.posted_on %></b></p>
<br>
<p><%= vblog.post %></p>
<br>
<br>
<%= link_to 'View', vblog %>
<% if @user.role == "admin" %>
<%= link_to 'Change/Update', edit_vblog_path(vblog) %> |
<%= link_to 'Delete', vblog, method: :delete, data: { confirm: 'Are you sure?' } %>
<br>
<%= link_to 'Create A New Blog Post', new_vblog_path %>
Run Code Online (Sandbox Code Playgroud)
我的vblogs_controller.rb文件如下所示:
class VblogsController < ApplicationController
before_action :set_vblog, only: [:show, :edit, …Run Code Online (Sandbox Code Playgroud) 我在我的显示操作中的帖子控制器中收到错误消息"错误的参数数量错误(1为0).我将评论该特定行的结尾.感谢您的帮助.
def show
@post = Post.all(:order => 'created_at DESC') #this is the error line
end
def new
@post = Post.new
end
def create
@post = Post.new(params[:post])
if @post.save
redirect_to @post
else
render :new
end
end
Run Code Online (Sandbox Code Playgroud) 在Rails 4.2.0.beta1上我收到一个错误:
如果没有摘要,则不应直接请求资产:使用ActionView :: Helpers中的帮助程序来请求fonts/source-sans-pro.woff
样式表:
@font-face {
font-family: 'Source Sans Pro';
font-style: normal;
font-weight: 400;
src: local('Source Sans Pro'), local('SourceSansPro-Regular'), url(/assets/source-sans-pro.woff) format('woff');
}
Run Code Online (Sandbox Code Playgroud)
配置是:
config.serve_static_assets = true
config.assets.js_compressor = :uglifier
config.assets.compile = true
config.assets.digest = true
config.assets.version = '1.0'
config.assets.paths << Rails.root.join('app', 'assets', 'fonts')
config.assets.precompile += %w(.svg .eot .woff .ttf)
Run Code Online (Sandbox Code Playgroud)
当然我可以禁用摘要,它可以再次工作,但我有兴趣使用它们.因此,如何在需要时使用摘要source-sans-pro.woff?
请注意,我将字体放在assets/fonts目录中,而不是public/目录中.我没有看到图像和字体之间的区别,所以我想将它们保存在同一目录下 - app/assets.
@open = Array.new
@close = Array.new
@posts.each do |post|
if !post.status.nil? and post.status == 'Open'
@open.push(post)
else
@close.push(post)
end
end
Run Code Online (Sandbox Code Playgroud)
我可以用不那么冗长的方式写出来吗?
我正在iOS中的Android中使用Rails 4 API后端和移动应用程序.使用Nginx和Unicorn.
要获取某些数据,cliens必须在请求的标题中向我发送(以及其他内容)用户ID.我们正在使用自定义标头,例如WWW_CUSTOM_NAME,此处说明.我知道在Rails 4中,我可以通过执行类似的操作捕获这些自定义标头中的值
request.headers["HTTP_WWW_CUSTOM_NAME"]
Run Code Online (Sandbox Code Playgroud)
事实上,我和我的团队每次都在开发中工作.但是当我们将代码推送到生产服务器时,这种方法并不起作用.
这几乎就像自定义标题在生产中消失了.我尝试了同样的事情,没有额外的"HTTP_",删除大写字母,没有任何效果.但在开发工作中没问题.
知道为什么一定是这个吗?