我有一個軌道應用程序。我在users /:id/show頁面上顯示來自不同類的更多部分(用戶,任務,對話,消息)。我在users.controller的def show action中設置了所有實例變量(對於其他類)。在同一頁面上更多的軌道部分
它似乎有點沉重,所以有比這更好的方法嗎? (我使用@task和@message的AJAX調用。)
def show
@user = User.find(params[:id])
if Task.between(current_user.id, @user.id).present?
@tasks = Task.uncompleted.between(current_user.id, @user.id).order("created_at DESC").includes(:assigner).paginate(page: params[:page], per_page: 12)
@task = Task.new
if Conversation.between(current_user.id, @user.id).present?
@conversation = Conversation.between(current_user.id, @user.id).first
@messages = @conversation.messages.includes(:user)
@message = Message.new
respond_to do |format|
format.html
format.js { render :template => "tasks/update.js.erb", :template => "tasks/destroy.js.erb", layout: false }
end
end
else
redirect_to user_profile_path(@user)
end
end
更新:
用戶/顯示:
<%if @conversation%>
<%= render 'conversations/show' %>
<% end %>
<tbody class="newtaskinsert2">
<%= render partial: "tasks/task_between", collection: @tasks, as: :task %>
</tbody>
對話/ _show:
<div class="chatboxcontent">
<% if @messages.any? %>
<%= render @messages %>
<% end %>
</div>
<div class="chatboxinput">
<%= form_for([@conversation, @message], :remote => true, :html => {id: "conversation_form_#{@conversation.id}"}) do |f| %>
<%= f.text_area :body, class: "chatboxtextarea", "data-cid" => @conversation.id %>
<% end %>
<%= form_for([@conversation, @message], html: {class: "refile_form"}, remote: true) do |form| %>
<span class="btn btn-success btn-sm btn-file">Choose file
<%= form.attachment_field :message_attachment, direct: true, presigned: true, class: "choosefile" %></span>
<%= form.submit "Send File", class: "btn btn-primary btn-sm btn-submit-refile", style:"display:none"%>
<% end %>
<span id="progresspercent"></span>
</div>
謝爾蓋,謝謝你長時間的回答。你能通過代碼示例告訴我你將如何重構我的用戶/ show視圖和控制器?我更新了我的問題。正如您在users/show頁面上看到的,我呈現任務和對話,並且在呈現的對話中,我將顯示消息/消息表單。 –
我已經更新了我的答案 –