0

我有一個軌道應用程序。我在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> 

回答

1

您只能在控制器中保留@user實例變量,並且部分使用:@ user.tasks而不是@tasks,@ user.tasks.new而不是@task等等。還要注意,你可以傳遞參數給諧音(3.4.4 Passing Local Variables

對於樣本:

<%= render partial: "your_partial", locals: {tasks: @user.tasks} %> 

更新:

用自己的方式(從類而不是對象調用的方法),你可以做類似的東西:

def show 
    @user = User.find(params[:id]) 
    if Task.between(current_user.id, @user.id).present? 
    # @user.tasks.where(another_user_field_name: current_user).present? - looks more like Rails way 
     @tasks = Task.uncompleted.between(current_user.id, @user.id).order("created_at DESC").includes(:assigner).paginate(page: params[:page], per_page: 12) 
     @conversation = Conversation.between(current_user.id, @user.id).first  
     if @conversation 
     respond_to do |format| 
      format.html 
      format.js { render :template => "tasks/update.js.erb", :template => "tasks/destroy.js.erb", layout: false } 
     end 
     end 
     # Do not forget that if @conversation is not exists this code render views by default way 
    else 
     redirect_to user_profile_path(@user) 
    end 
    end 


<%= render 'conversations/show' %> 
<tbody class="newtaskinsert2"> 
    <%= render partial: "tasks/task_between"%> 
</tbody> 


<%if @conversation%> 
    <div class="chatboxcontent"> 
    <%= render '_your_messages_partial', locals: {messages: @conversation.messages.includes(:user)}%> 
    </div> 
    <div class="chatboxinput"> 
    <%= form_for([@conversation, @conversation.messages.new], :remote => true, :html => {id: "conversation_form_#{@conversation.id}"}) do |f| %> 
     <%= f.text_area :body, class: "chatboxtextarea", "data-cid" => @conversation.id %> 
    <% end %> 
    <%= form_for([@conversation, @conversation.messages.new], 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> 
<% end %> 

您可以使用關係的情況下,使代碼更短(@ user.conversations而不是Conversation.between ...等)

+0

謝爾蓋,謝謝你長時間的回答。你能通過代碼示例告訴我你將如何重構我的用戶/ show視圖和控制器?我更新了我的問題。正如您在users/show頁面上看到的,我呈現任務和對話,並且在呈現的對話中,我將顯示消息/消息表單。 –

+0

我已經更新了我的答案 –