gpt4 book ai didi

ruby-on-rails - 当不在括号内时,Ruby 三元条件似乎被忽略了?

转载 作者:数据小太阳 更新时间:2023-10-29 08:18:03 24 4
gpt4 key购买 nike

我试图理解为什么将此 if-then-else 行转换为三元表达式 (?:) 不起作用。我知道这可能不是最合适的代码,但我想了解发生了什么。

   if a = Artist.find_by(id: params[:artist_id]) then @songs = a.songs else redirect_to(artists_path, alert: "Artist not found") end

上面的 if-then-else 通过了测试,一切都很好,但是当我试图将它变成像这样的三元表达式时:,它无法像我预期的那样工作。

  a = Artist.find_by(id: params[:artist_id]) ? @songs = a.songs : redirect_to(artists_path, alert: "Artist not found")

这给了我以下失败:

1) songs when nested under artists /artists/:artist_id/songs displays the songs with valid artist
Failure/Error: a = Artist.find_by(id: params[:artist_id]) ? @songs = a.songs : redirect_to(artists_path, alert: "Artist not found")

NoMethodError:
undefined method `songs' for nil:NilClass
# ./app/controllers/songs_controller.rb:4:in `index'
# /home/dapawn/.rvm/gems/ruby-2.3.3/gems/rack-1.6.4/lib/rack/etag.rb:24:in `call'
# /home/dapawn/.rvm/gems/ruby-2.3.3/gems/rack-1.6.4/lib/rack/conditionalget.rb:25:in `call'
# /home/dapawn/.rvm/gems/ruby-2.3.3/gems/rack-1.6.4/lib/rack/head.rb:13:in `call'
# /home/dapawn/.rvm/gems/ruby-2.3.3/gems/rack-1.6.4/lib/rack/session/abstract/id.rb:225:in `context'
# /home/dapawn/.rvm/gems/ruby-2.3.3/gems/rack-1.6.4/lib/rack/session/abstract/id.rb:220:in `call'
# /home/dapawn/.rvm/gems/ruby-2.3.3/gems/railties-4.2.5/lib/rails/rack/logger.rb:38:in `call_app'
# /home/dapawn/.rvm/gems/ruby-2.3.3/gems/railties-4.2.5/lib/rails/rack/logger.rb:20:in `block in call'
# /home/dapawn/.rvm/gems/ruby-2.3.3/gems/railties-4.2.5/lib/rails/rack/logger.rb:20:in `call'
# /home/dapawn/.rvm/gems/ruby-2.3.3/gems/rack-1.6.4/lib/rack/methodoverride.rb:22:in `call'
# /home/dapawn/.rvm/gems/ruby-2.3.3/gems/rack-1.6.4/lib/rack/runtime.rb:18:in `call'
# /home/dapawn/.rvm/gems/ruby-2.3.3/gems/rack-1.6.4/lib/rack/lock.rb:17:in `call'
# /home/dapawn/.rvm/gems/ruby-2.3.3/gems/rack-1.6.4/lib/rack/sendfile.rb:113:in `call'
# /home/dapawn/.rvm/gems/ruby-2.3.3/gems/railties-4.2.5/lib/rails/engine.rb:518:in `call'
# /home/dapawn/.rvm/gems/ruby-2.3.3/gems/railties-4.2.5/lib/rails/application.rb:165:in `call'
# /home/dapawn/.rvm/gems/ruby-2.3.3/gems/rack-1.6.4/lib/rack/urlmap.rb:66:in `block in call'
# /home/dapawn/.rvm/gems/ruby-2.3.3/gems/rack-1.6.4/lib/rack/urlmap.rb:50:in `each'
# /home/dapawn/.rvm/gems/ruby-2.3.3/gems/rack-1.6.4/lib/rack/urlmap.rb:50:in `call'
# /home/dapawn/.rvm/gems/ruby-2.3.3/gems/rack-test-0.6.3/lib/rack/mock_session.rb:30:in `request'
# /home/dapawn/.rvm/gems/ruby-2.3.3/gems/rack-test-0.6.3/lib/rack/test.rb:244:in `process_request'
# /home/dapawn/.rvm/gems/ruby-2.3.3/gems/rack-test-0.6.3/lib/rack/test.rb:58:in `get'
# /home/dapawn/.rvm/gems/ruby-2.3.3/gems/capybara-2.5.0/lib/capybara/rack_test/browser.rb:60:in `process'
# /home/dapawn/.rvm/gems/ruby-2.3.3/gems/capybara-2.5.0/lib/capybara/rack_test/browser.rb:35:in `process_and_follow_redirects'
# /home/dapawn/.rvm/gems/ruby-2.3.3/gems/capybara-2.5.0/lib/capybara/rack_test/browser.rb:21:in `visit'
# /home/dapawn/.rvm/gems/ruby-2.3.3/gems/capybara-2.5.0/lib/capybara/rack_test/driver.rb:42:in `visit'
# /home/dapawn/.rvm/gems/ruby-2.3.3/gems/capybara-2.5.0/lib/capybara/session.rb:232:in `visit'
# /home/dapawn/.rvm/gems/ruby-2.3.3/gems/capybara-2.5.0/lib/capybara/dsl.rb:51:in `block (2 levels) in <module:DSL>'
# ./spec/features/songs_spec.rb:13:in `block (4 levels) in <top (required)>'

如果 anil 它怎么能完成三元表达式的真实部分?

如果我像这样在条件周围加上括号,它就可以正常工作:

   (a = Artist.find_by(id: params[:artist_id])) ? @songs = a.songs : redirect_to(artists_path, alert: "Artist not found")

我认为它一定是某种优先事项,但我看不到它。提前感谢您花时间回复我并帮助我学习。

最佳答案

这是一个优先事项。 ?: 的优先级高于=,允许您根据条件分配:

animal = is_dog ? "dog" : "not a dog"

可以理解为

animal = (is_dog ? "dog" : "not a dog")

如果你想在条件内赋值,你需要括号:

(animal = find_animal("dog")) ? "found dog" : "no dog"

在你的情况下,

a = Artist.find_by(id: params[:artist_id]) ?
@songs = a.songs :
redirect_to(artists_path, alert: "Artist not found")

它将首先评估 Artist.find_by 并据此决定条件;如果找到某些东西,它会尝试评估 @songs = a.songs,但 a 尚未分配a 正在等待条件的结果,并且会收到 @songs = a.songs 的值或 redirect_to 的值。 . 当条件完成时。

从风格上来说,我宁愿将它保留为 if ... then ... else ... end,因为您没有使用结果?:。三元条件运算符通常在条件返回的值相关时使用,并且两个分支都是相对简单的表达式。我会使用 if ... then ... else ... end 如果没有使用返回值(就像在你的代码中),或者如果分支不仅仅包含一个简单的表达式,尤其是如果是多行或多语句:

foo =
if bar
do_something
do_something_else(bar)
final_result
else
do_another_thing
skip_something_else
the_other_final_result
end

关于ruby-on-rails - 当不在括号内时,Ruby 三元条件似乎被忽略了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52660958/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com