簡體   English   中英

Ruby on Rails 兩個同時對兩個不同的控制器創建操作

[英]Ruby on Rails two create actions to two different controllers simultaneously

我有一個線程 controller 和消息 controller。

線程 has_many 消息

用戶單擊發送后,我將數據發送到線程 controller 以創建線程。 我想做到這樣

threads_controller.rb

def create
    ...
    if @thread.save
         #send data into messages_controller 
         #to create the corresponding table for a message with this thread_id

end

因此,如果第一個成功,基本上我會嘗試一個接一個地執行兩個 POST。

我認為 redirect_to 是我應該使用的。 是否可以使用 redirect_to 傳遞參數並從不同的 controller 調用創建操作?

編輯:我必須有一個線程(由於 Ryan 提到的原因,名稱選擇不當,但為了不讓人們混淆底部的答案,讓我們保留它)Model 和消息 Model 在這里。 線程表只需要傳入message_title。 Message 表接收 from_id(用戶發送消息的 id)、to_id(用戶接收消息的 id)和 message_content。 我正在嘗試以一種接受 message_title 和 message_content 的形式來完成所有這些工作。

我希望這有助於理解這個問題。

感謝大家

我認為你正在以錯誤的方式解決這個問題。

第一:我真的希望你不要調用 model Thread ,因為這會與 Ruby class Thread沖突。 如果是,請選擇不同的詞

現在有了“請將槍對准的腳”的信息......


您不應該調用MessagesController為 controller 創建新消息。相反,您應該在新的線程形式中使用嵌套屬性:

<%= form_for @thread do |f| %>
  <%= f.fields_for :messages do |message| %>
    <%= render "messages/form", :message => message %>
  <% end %>
<% end %>

在您的DiscussionThread (我在這里假設它的名稱)model 中,您將擁有這些行:

has_many :messages
accepts_nested_attributes_for :messages

您可能還必須將messages_attributes添加到此 model 中的attr_accessible屬性。

這告訴DiscussionThread model 實例也可以接受messages關聯的屬性。 在您的ThreadsController中,操作將保持不變。

有關嵌套屬性的更多信息,我建議觀看Nested Forms #1Nested Forms #2上的 Railscast。

如果我理解你的問題,你只需要調用Message model的create方法並傳入相關信息即可。 所以在你的 if @thread.save 中是這樣的:

Message.create(:item_1 => params[:item_1])

如果您不想 go 這條路線,您可以使用嵌套資源,然后讓 Rails 在您傳遞正確信息時自動創建新消息。

沒有理由將 go 換成另一個 controller。您可以在線程 controller go 中:

@thread.messages << Message.new(...)

或者在消息 controller 中執行此操作,這對我來說更有意義,因為您的用戶正在創建消息,而線程創建作為副作用。 如果您正確發送thread_id ,在params[:message][:thread_id]中,關聯將在您創建 object 時自動建立:

@message = Message.create(params[:message]) 

如果您需要一些其他邏輯來決定消息與哪個線程相關聯,只需直接設置thread_id即可:

@message.thread_id = current_thread.id # Or Thread.create(...) or whatever
@message.save

我將您的基礎用例視為:

  • 創建新消息和線程
  • 在現有線程中創建新消息
  • 查看消息
  • 查看線程和所有關聯的消息

我會建議多對多關系: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_and_belongs_to_many

# define has and belongs to many relationship

class Message
  has_and_belongs_to_many :threads
end

class Thread
  has_and_belongs_to_many :messages
end

class MessageThread
  belongs_to :message
  belongs_to :thread
end

# use case: creating a new message and a new thread, showing message

class MessagesController < ApplicationController
  def create
    @message = current_user.messages.create! :text => params[:text]
    @thread = @message.threads.create!
  end

  def show; @message = current_user.messages.find(params[:id); end;
end

# use case: creating a message in an existing thread

def MessagesThreadsController < ApplicationController
  def create
    @thread = current_user.threads.find params[:id]
    @thread.messages.create! :text => params[:text]
  end
end

# use case: viewing all messages in a thread

def ThreadsController < ApplicationController
  before_filter :set_thread

  def show
    @thread = current_user.threads.find params[:id]
    @messages = @thread.messages
  end
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM