在我的主页上,我使用 Papertrail 显示最新更新。
控制器
@contributions = PaperTrail::Version.all.order("created_at DESC")
Run Code Online (Sandbox Code Playgroud)
看法
<% @contributions.each do |v| %>
<h2><%= v.item.name %><small>by </small></h2>
<% v.changeset.each do |data| %>
<h4><%= data %></h4>
<% end %>
<%= v.whodunnit %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
在这里,我得到了关联的用户,但只有 whodunnit 的 ID,但我想得到一个用户实例来获取用户名。所以insted v.whodunnit 我想要v.user.username。
我一直在看一个类似的问题,但似乎无法找到一种方法来创建版本和用户之间的关系。
用户模型
class User < ActiveRecord::Base
has_many :versions, :foreign_key => 'whodunnit', :class_name => "PaperTrail::Version"
end
Run Code Online (Sandbox Code Playgroud)
版本型号
class Version < PaperTrail::Version
belongs_to :user, :foreign_key => 'whodunnit'
end
Run Code Online (Sandbox Code Playgroud)
编辑
当我看到 <%= v.user %> 时,我得到了这个
undefined method `user' …Run Code Online (Sandbox Code Playgroud) 我在这样的匹配上嵌套了结果
/匹配/ 16 /结果/ 13 /编辑
我有以下选择字段,这显示正确的信息(team.name及其team.id)
<%= f.collection_select :winner, @select_winner_loser, :id, :name %>
Run Code Online (Sandbox Code Playgroud)
现在,当我尝试编辑我的结果并选择获胜者时,我得到了这个:
ActiveRecord :: AssociationTypeMismatch 团队(#10504340)预期,得到字符串(#6163240)
"当分配给关联的对象具有不正确的类型时引发." http://api.rubyonrails.org/classes/ActiveRecord/AssociationTypeMismatch.html
Winner是一个团队对象,result.rb看起来像这样
class Result < ActiveRecord::Base
has_one :match
belongs_to :winner, class_name: "Team"
belongs_to :loser, class_name: "Team"
end
Run Code Online (Sandbox Code Playgroud)
@select_winner_loser来自我的results_controller
def edit
@select_winner_loser = []
@select_winner_loser << @match.top
@select_winner_loser << @match.bottom
end
Run Code Online (Sandbox Code Playgroud)
Match.top和bottom也是团队对象
class Match < ActiveRecord::Base
belongs_to :top, class_name: "Team"
belongs_to :bottom, class_name: "Team"
...
belongs_to :result
end
Run Code Online (Sandbox Code Playgroud)
我不知道为什么我会得到这个,因为我在选择字段中有正确的对象,任何想法?
谢谢
这是我的HTML表单
<form method="post">{% csrf_token %}
<strong>Start</strong><br />
Lng: <input type="text" id="start_lng"><br />
Lat: <input type="text" id="start_lat"><br />
<strong>Destination</strong><br />
Lng: <input type="text" id="dest_lng"><br />
Lat: <input type="text" id="dest_lat"><br />
<input type="submit" value="Go" />
</form>
Run Code Online (Sandbox Code Playgroud)
但是,当我查看我的请求信息时,我得到的唯一POST值是"csrfmiddlewaretoken",而不是start_lng,start_lat等等.