如何解決了以下問題:更新複雜的鏈接表
我想一個新行添加到表submitted_pictures,它鏈接如下:
game.rb
has_many :rounds
has_many :participants, :dependent => :destroy
has_many :submitted_pictures, :through => :rounds
has_many :users, :through => :participants
accepts_nested_attributes_for :participants
accepts_nested_attributes_for :rounds, :reject_if => :all_blank
round.rb
belongs_to :game
has_many :submitted_pictures, :dependent => :destroy
accepts_nested_attributes_for :submitted_pictures
submitted_picture.rb
has_one :round
has_one :game, :through => :rounds
belongs_to :user
所以我可以打電話:
<% @user.games.rounds.last.submitted_pictures.each do |blabla| %><% end>
我提出使用複雜的形式:
<%= form_for(@game) do |f| %>
<%= f.fields_for :round do |ff| %>
<%= ff.fields_for :submitted_pictures do |fff| %>
<%= fff.label :flickr_id %>
<%= fff.text_field :flickr_id %>
<% end %>
<% end %>
<%= f.submit "Submit Picture", class: "btn btn-primary" %>
<% end %>
希望能增加與flickr_id新submitted_picture(其持有httplink現在),連接到當前遊戲(@game)。
我一直在試圖幾件事情來更新它,但它似乎沒有讓步:(該update_attributes方法是完全錯誤的,我現在看到:P)
def update
@game = Game.find(params[:id])
if @game.rounds.last.submitted_pictures.update_attributes(params[:id])
flash[:success] = "Pic Submitted!"
else
render :action => 'new'
end
end
而且
def update
@game = Game.find(params[:id])
if @game.save
flash[:success] = "Pic Submitted!"
redirect_to games_path
else
render :action => 'new'
end
結束
我無法得到它的工作。我遇到各種各樣的錯誤,所以不要在這裏注意它們,我認爲最好是要求最好的解決方案。
因此,在短期,我想一個submitted_picture加入到遊戲中的最新一輪(最近created_at)。
這正是我想要的。我認爲製作這些表單是最好的Rails方式,但它確實非常複雜。我現在正在研究它,但仍然讓我感到困惑,你正在使用更新,新建,創建或w/e – Flame
我已經遇到了一些事情:即時通訊有一個顯示頁面的遊戲,並且該視圖有一個按鈕開始新一輪和提交圖片的按鈕。我如何將這些鏈接到不同的操作?如果你不知道要找什麼,在Google上找到這些東西是相當困難的:P – Flame