Rails 3.2 奇怪的观点
Rails 3.2 weird views
第一次在这个网站上提问,希望大家能帮帮我'problem'。 :-)
其实在开发中并不是真正的问题,而是在生产中...
这是我的问题:
我正在使用 Rails 3.2.11 和 ruby 2.1.7。为此,我使用 RubyMine。
从一开始,当我在哈希中推送一些数据时,记录也出现在集合的用户实例中。
我在我的配置中搜索过,在我的 development.rb 中搜索过,初始值设定项...但我不知道它来自哪里! :-(
因为我在这个网站上没有任何声誉,所以我不能告诉你它是什么,所以我会用其他方式解释。
我通过单击 'sign in' 通过视图 将我的用户 'Test1' 添加到锦标赛 。
我的观点(我使用的是 Haml):
app/views/tournaments/show.html.haml
%p#notice= notice
%p
%b Name:
= @tournament.name
%p
%b Place:
= @tournament.place
= @tournament.to_coordinates
%p
= image_tag "http://maps.google.com/maps/api/staticmap?size=450x300&sensor=false&zoom=16&markers=#{@tournament.latitude}%2C#{@tournament.longitude}"
%p
%b Nb players max:
= @tournament.nb_players_max
- if can? :update, @tournament
= link_to 'Add Match', new_match_path(@tournament.id)
|
= link_to 'Add Games', show_games_path(@tournament.id)
|
= link_to 'Edit', edit_tournament_path(@tournament)
|
= link_to 'Back', tournaments_path
- if @tournament.users.include?(@current_user)
%p
you\'re already signed in for this tournament
- else
= form_for @tournament, url: add_player_path, method: :post do |f|
= f.hidden_field 'tournament_id', :value => @tournament.id
= f.submit 'Sign in'
= link_to 'Seed players', seed_players_path(@tournament.id), method: :post
- if @tournament.users.any?
%ul.list-group
= @tournament.users.each do |player|
%li.list-group-item
= player.pseudo
- else
%p
No players for this tournament.
- if @tournament.games.any?
= @tournament.games.each do |game|
%h2
= game.title
= link_to 'Sign in all players'
- else
%p
No games for this tournament. A new game will be added rapidly.
通过单击登录按钮,用户已正确添加到@tournament.users。但这也出现在下面的视图中:
- if @tournament.users.any?
%ul.list-group
= @tournament.users.each do |player|
%li.list-group-item
= player.pseudo
- else
%p
No players for this tournament.
[#<User id: 29, country: "BE", nb_victory: nil, nb_defeat: nil, total_points: nil, created_at: "2016-02-16 14:20:36", updated_at: "2016-02-18 10:07:49", first_name: "T", last_name: "T", pseudo: "Test1", email: "test1@test.com", encrypted_password: "a$WRHkX0mN0t1GAEYg8CQPJeVHBg0bVzBmDxpHKtU.g23A...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 3, current_sign_in_at: "2016-02-18 10:07:49", last_sign_in_at: "2016-02-17 09:31:06", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", confirmation_token: "Q2rJ8aWYPmttd9XRjKgX", confirmed_at: nil, confirmation_sent_at: "2016-02-16 14:20:36", unconfirmed_email: nil, avatar: "chat.png", provider: nil, uid: nil, role: "player">]
在 HTML 代码中,我可以看到:
<ul class="list-group">
<li class="list-group-item"> Test1 </li>
[#<User id: 29, country: "BE", nb_victory: nil, nb_defeat: nil, total_points: nil, created_at: "2016-02-16 14:20:36", updated_at: "2016-02-18 10:07:49", first_name: "T", last_name: "T", pseudo: "Test1", email: "test1@test.com", encrypted_password: "a$WRHkX0mN0t1GAEYg8CQPJeVHBg0bVzBmDxpHKtU.g23A...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 3, current_sign_in_at: "2016-02-18 10:07:49", last_sign_in_at: "2016-02-17 09:31:06", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", confirmation_token: "Q2rJ8aWYPmttd9XRjKgX", confirmed_at: nil, confirmation_sent_at: "2016-02-16 14:20:36", unconfirmed_email: nil, avatar: "chat.png", provider: nil, uid: nil, role: "player">]
</ul>
这是怎么回事?好像是配置问题。
第一:请您把问题说清楚,免得我们瞎猜。
您的问题似乎是向您 html 写入了您不想要的数据。它看起来像一个 ruby 数组:[#<User id: 29, country: "BE"....
这是哪里出了问题?
将 =
替换为 -
之前的 @tournament.users.each
。
原因:each
调用遍历数组,但在完成后 return 遍历整个数组。如果您使用“=”,此 return 值将写入您的 html.
MurifoX 是对的!
Did you change = @tournament.users.each to - @tournaments.users.each?
当然,如果您在 HAML 中使用“=”,它会被翻译成“<%=”。但是,如果我们使用“-”而不是“=”,它会被翻译成“<%”。
原因(来自梅尔):
reason: The each call iterates over the array, but also returns the whole array after finishing. If you use a '=', this return value will be written into your html.
就是这样!谢谢大家:-)
ps: 当我能做到的时候,我会把你的答案放在上面:)
编辑:
戴夫牛顿也是对的^^
第一次在这个网站上提问,希望大家能帮帮我'problem'。 :-)
其实在开发中并不是真正的问题,而是在生产中...
这是我的问题:
我正在使用 Rails 3.2.11 和 ruby 2.1.7。为此,我使用 RubyMine。 从一开始,当我在哈希中推送一些数据时,记录也出现在集合的用户实例中。
我在我的配置中搜索过,在我的 development.rb 中搜索过,初始值设定项...但我不知道它来自哪里! :-(
因为我在这个网站上没有任何声誉,所以我不能告诉你它是什么,所以我会用其他方式解释。
我通过单击 'sign in' 通过视图 将我的用户 'Test1' 添加到锦标赛 。 我的观点(我使用的是 Haml):
app/views/tournaments/show.html.haml
%p#notice= notice
%p
%b Name:
= @tournament.name
%p
%b Place:
= @tournament.place
= @tournament.to_coordinates
%p
= image_tag "http://maps.google.com/maps/api/staticmap?size=450x300&sensor=false&zoom=16&markers=#{@tournament.latitude}%2C#{@tournament.longitude}"
%p
%b Nb players max:
= @tournament.nb_players_max
- if can? :update, @tournament
= link_to 'Add Match', new_match_path(@tournament.id)
|
= link_to 'Add Games', show_games_path(@tournament.id)
|
= link_to 'Edit', edit_tournament_path(@tournament)
|
= link_to 'Back', tournaments_path
- if @tournament.users.include?(@current_user)
%p
you\'re already signed in for this tournament
- else
= form_for @tournament, url: add_player_path, method: :post do |f|
= f.hidden_field 'tournament_id', :value => @tournament.id
= f.submit 'Sign in'
= link_to 'Seed players', seed_players_path(@tournament.id), method: :post
- if @tournament.users.any?
%ul.list-group
= @tournament.users.each do |player|
%li.list-group-item
= player.pseudo
- else
%p
No players for this tournament.
- if @tournament.games.any?
= @tournament.games.each do |game|
%h2
= game.title
= link_to 'Sign in all players'
- else
%p
No games for this tournament. A new game will be added rapidly.
通过单击登录按钮,用户已正确添加到@tournament.users。但这也出现在下面的视图中:
- if @tournament.users.any?
%ul.list-group
= @tournament.users.each do |player|
%li.list-group-item
= player.pseudo
- else
%p
No players for this tournament.
[#<User id: 29, country: "BE", nb_victory: nil, nb_defeat: nil, total_points: nil, created_at: "2016-02-16 14:20:36", updated_at: "2016-02-18 10:07:49", first_name: "T", last_name: "T", pseudo: "Test1", email: "test1@test.com", encrypted_password: "a$WRHkX0mN0t1GAEYg8CQPJeVHBg0bVzBmDxpHKtU.g23A...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 3, current_sign_in_at: "2016-02-18 10:07:49", last_sign_in_at: "2016-02-17 09:31:06", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", confirmation_token: "Q2rJ8aWYPmttd9XRjKgX", confirmed_at: nil, confirmation_sent_at: "2016-02-16 14:20:36", unconfirmed_email: nil, avatar: "chat.png", provider: nil, uid: nil, role: "player">]
在 HTML 代码中,我可以看到:
<ul class="list-group">
<li class="list-group-item"> Test1 </li>
[#<User id: 29, country: "BE", nb_victory: nil, nb_defeat: nil, total_points: nil, created_at: "2016-02-16 14:20:36", updated_at: "2016-02-18 10:07:49", first_name: "T", last_name: "T", pseudo: "Test1", email: "test1@test.com", encrypted_password: "a$WRHkX0mN0t1GAEYg8CQPJeVHBg0bVzBmDxpHKtU.g23A...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 3, current_sign_in_at: "2016-02-18 10:07:49", last_sign_in_at: "2016-02-17 09:31:06", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", confirmation_token: "Q2rJ8aWYPmttd9XRjKgX", confirmed_at: nil, confirmation_sent_at: "2016-02-16 14:20:36", unconfirmed_email: nil, avatar: "chat.png", provider: nil, uid: nil, role: "player">]
</ul>
这是怎么回事?好像是配置问题。
第一:请您把问题说清楚,免得我们瞎猜。
您的问题似乎是向您 html 写入了您不想要的数据。它看起来像一个 ruby 数组:[#<User id: 29, country: "BE"....
这是哪里出了问题?
将 =
替换为 -
之前的 @tournament.users.each
。
原因:each
调用遍历数组,但在完成后 return 遍历整个数组。如果您使用“=”,此 return 值将写入您的 html.
MurifoX 是对的!
Did you change = @tournament.users.each to - @tournaments.users.each?
当然,如果您在 HAML 中使用“=”,它会被翻译成“<%=”。但是,如果我们使用“-”而不是“=”,它会被翻译成“<%”。
原因(来自梅尔):
reason: The each call iterates over the array, but also returns the whole array after finishing. If you use a '=', this return value will be written into your html.
就是这样!谢谢大家:-)
ps: 当我能做到的时候,我会把你的答案放在上面:)
编辑: 戴夫牛顿也是对的^^