Rails - 形式的条件语句识别错误的 url 参数

Rails - Conditional statement in form recognizing wrong url paramater

我在附件与用户和团队之间建立了多态关系,以便用户和团队都可以上传附件。单击 "Add file" 按钮时,:team_id 或 :user_id 作为参数传递。然后,attachments#new 表单中的隐藏字段告诉控制器请求是来自团队还是用户,以便 attribute_update 可以应用于正确的模型。然而,在实践中,团队请求作为用户子操作传递,反之亦然。关于可能出问题的任何想法?

class Attachment < ActiveRecord::Base
  belongs_to :upload, polymorphic: true
end

class User < ActiveRecord::Base
  has_many :attachments, as: :upload
end

class Team < ActiveRecord::Base
  has_many :attachments, as: :upload
end

团队#show

<%= link_to "Add Files", new_attachment_path(:team_id => @team.id), class: "btn btn-md" %>

用户#show

<%= link_to "Add Files", new_attachment_path(:user_id => current_user), class: "btn btn-md" %>

附件#new

<% provide(:title, 'Add file') %>

<div class="row">
  <div class="col-md-6 col-md-offset-3">
    <%= form_for(@attachment) do |f| %>
      <%= render 'shared/error_messages', object: f.object %>

      <%= f.file_field :file %>

      <% if request.path == new_attachment_path(params[:team_id]) %>
        <%= hidden_field_tag(:subaction, 'Team') %>
      <% elsif request.path == new_attachment_path(params[:user_id]) %>
        <%= hidden_field_tag(:subaction, 'User') %>
      <% end %>

      <%= f.submit "Add Files", class: "btn btn-primary" %>
    <% end %>
  </div>
</div>

附件控制器

def create
    @attachment = Attachment.create(attachment_params)
    @team = Team.find_by(params[:team_id])
    @user = User.find_by(params[:user_id])
    if @attachment.save
      if params[:subaction] == 'Team'
        @attachment.update_attribute(:upload, @team)
        flash[:success] = "Team file uploaded!"
        redirect_to @team
      elsif params[:subaction] == 'User'
        @attachment.update_attribute(:upload, @user)
        flash[:success] = "User file uploaded!"
        redirect_to current_user
      end
    else
      render 'new'
    end
end

我认为问题在于您在 attachments#new 中的 if 语句。 params[:team_id] 和 params[:user_id] 都会 return 一个整数,所以你的 if 语句不能真正判断请求是来自团队还是一个用户。作为建议,您可以将 params[:team_id] 存储在诸如 @team_id 之类的变量中,然后将您的 if 语句更改为如下内容:

<% unless @team_id.nil? %>
  <%= hidden_field_tag(:subaction, 'Team') %>
<% else%>
  <%= hidden_field_tag(:subaction, 'User') %>
<% end %>

但是,您可以通过在模型中使用多态关联来进一步改进这一点,如下所述link:https://launchschool.com/blog/understanding-polymorphic-associations-in-rails

更改了 url 参数以传递字符串 'User' 或 'Team' 而不是 team_id 或 user_id 以允许控制器 if 语句正常运行.

团队#show

<%= link_to "Add Files", new_attachment_path(:upload_type => 'Team'), class: "btn btn-md" %>

用户#show

<%= link_to "Add Files", new_attachment_path(:upload_type => 'User'), class: "btn btn-md" %>

附件#new

<% provide(:title, 'Add file') %>

<div class="row">
  <div class="col-md-6 col-md-offset-3">
    <%= form_for(@attachment) do |f| %>
      <%= render 'shared/error_messages', object: f.object %>

      <%= f.file_field :file %>

      <%= hidden_field_tag(:subaction, params[:upload_type]) %>

      <%= f.submit "Add Files", class: "btn btn-primary" %>
    <% end %>
  </div>
</div>