我试图做一个简单的一个衬垫,而使用花括号循环在红宝石.我以下列格式成功:
while x < 5 do x+=1 end
Run Code Online (Sandbox Code Playgroud)
这足以作为一个衬里,但我不是在一个衬里中使用do end的粉丝.我想做类似的事情:
while x < 5 { x += 1 }
Run Code Online (Sandbox Code Playgroud)
可以这样做吗?
我正在编辑资源 - @article- 每当我提交它时,我都会收到以下错误:
JSON::ParserError in ArticlesController#update
783: unexpected token at '#<ImageUploader::UploadedFile:0x00007fb154adb508>'
Run Code Online (Sandbox Code Playgroud)
它突出显示了这一行:
if @article.update(article_params)
Run Code Online (Sandbox Code Playgroud)
这是我的Article#Update操作中的正常 Rails 样板代码:
def update
respond_to do |format|
if @article.update(article_params)
format.html { redirect_to @article, notice: 'Article was successfully updated.' }
format.json { render :show, status: :ok, location: @article }
else
format.html { render :edit }
format.json { render json: @article.errors, status: :unprocessable_entity }
end
end
end
Run Code Online (Sandbox Code Playgroud)
这是服务器日志的完整输出(包括参数):
Started PATCH "/articles/welcome-to-ftja" for ::1 at 2020-05-04 02:11:43 -0500
Processing by ArticlesController#update as HTML
Parameters: …Run Code Online (Sandbox Code Playgroud) 我正在研究一个项目,经常遇到有关therubyracer和libv8的问题,所以我决定删除它们.它似乎是唯一使用它们作为依赖的东西是较少的轨道,我想要删除无论如何.
我的主要问题是它们是什么,我是否需要它们在普通应用程序中,如果是这样,为什么?
我和Jennifer.cr在Amber框架上有一个水晶项目,我在我的控制器上收到了这个错误:
Can't infer the type of instance variable '@companies' of CompanyController
@companies = Company.all
Run Code Online (Sandbox Code Playgroud)
控制器是:
class CompanyController < ApplicationController
def index
@companies = Company.all
render("index.slang")
end
end
Run Code Online (Sandbox Code Playgroud)
当我尝试以这种方式解决问题时:
class CompanyController < ApplicationController
def index
@companies : Array(Company) = Company.all
render("index.slang")
end
end
Run Code Online (Sandbox Code Playgroud)
我有另一个错误:
instantiating 'CompanyController#index()'
in src/controllers/company_controller.cr:7: declaring the type of an instance variable must be done at the class level
@companies : Array(Company) = Company.all
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个"简单"问题?
我已经完成了一些在模型中使用 @ 属性变量(已经是 attr_accessible)的 Rails 应用程序。我有一个艰难的时间让这个但从信息我收集name和@name有同样的事情在一个模型,但IM在这个可能不正确。
这些@ 变量是如何工作的,为什么要使用@ 符号变量?
我想为数组创建一个方法来获得数组的splat.这可以用Ruby做吗?
例如,这是我当前的代码:
Array.module_eval do
def to_args
return *self
end
end
Run Code Online (Sandbox Code Playgroud)
我希望[1,2,3].to_args能回来,1,2,3但最终会回归[1,2,3]
Ruby 2.3引入了安全导航操作符,但我发现它的语法过于离散,在短暂扫描代码时很容易错过.相反,我更喜欢语法,try因为它看起来更加明显和有意.
所以我的问题是在Ruby 2.3+中,是否有一种方法可以将安全导航操作符的方法或方法修改为&.自定义方法名称即ie.s.fast_try(:upcase!).fast_try(:downcase)而不是写作s&.upcase!&.downcase
主要思想是尝试提高性能而不是其他实现,例如try方法.不,我不关心尝试和安全导航操作员之间的微小行为差异.此外,如果无法避免,我不介意一些模糊的论证限制,只需指出它们.
当我对服务器资源中的项执行删除操作时,它不会删除视图中的项.
风景:
%ul(class="phrases")
%li(ng-repeat="phrase in phrases")
%p.id {{phrase.id}}
%p.content {{phrase.content}}
%p.author {{phrase.author}}
%button(ng-click="deletePhrase(phrase.id)") Delete ^
Run Code Online (Sandbox Code Playgroud)
服务:
wrmcServices.factory 'Phrase', ($resource) ->
$resource('api/v1/phrases.json',{},
query:
method:'GET'
params:
phraseId:'phrases'
isArray:true
)
wrmcServices.factory 'Phrase', ($resource) ->
$resource('api/v1/phrases/:id.json',{id: "@id"},
update:
method:'PUT'
delete:
method: 'DELETE'
)
Run Code Online (Sandbox Code Playgroud)
控制器:
wrmcControllers.controller 'phrasesCtrl', ($scope, Phrase) ->
$scope.phrases = Phrase.query()
$scope.deletePhrase = (phraseId) ->
Phrase.delete({id: phraseId}, ->
$scope.phrases.splice(phraseId,1)
)
Run Code Online (Sandbox Code Playgroud)
我的临时修复是在deletePhrase函数的末尾放置$ scope.phrases = Phrase.query(),但我不喜欢页面刷新的方式.不应该只是从新视图中删除项目,因为我将其从数组中删除.
我正在关注创建指令的博客文章:http://joelhooks.com/blog/2014/02/11/lets-make-full-ass-angularjs-directives/
他似乎建议将控制器与指令分开.但是尝试在链接函数中从外部控制器运行函数导致undefined不是一个函数.
调节器
wrmcControllers.controller 'voteCtrl', ($scope,$attrs) ->
this.hasVoted = false
this.init = ->
if this.hasVoted
#scope.vote = Vote.get(scope.vote)
else
this.vote = null
this.voteUp = ->
if this.vote == true
this.vote = null
else
this.vote = true
this.hasVoted = true
this.voteDown = ->
if this.vote == false
this.vote = null
else
this.vote = false
this.hasVoted = true
Run Code Online (Sandbox Code Playgroud)
指示
wrmcDirectives.directive 'voteSet', ->
restrict: 'AE'
scope: {}
controller: 'voteCtrl'
templateUrl: 'vote.html'
link: (scope,element,attrs,controller) ->
console.log controller
controller.init()
Run Code Online (Sandbox Code Playgroud)
Console.log控制器输出
function () …Run Code Online (Sandbox Code Playgroud) ruby ×4
angularjs ×2
activerecord ×1
crystal-lang ×1
json ×1
libv8 ×1
ruby-2.7 ×1
simple-form ×1
therubyracer ×1
trix ×1