错误的值从form_tag和modal传递给控制器

时间:2019-11-13 16:21:37

标签: ruby-on-rails

我有一个包含此代码的索引视图...

 <% @jobs.each do |job| %>
   ...
   <div class="modal micromodal-slide" id="modal-1" aria-hidden="true">
          <div class="modal__overlay" tabindex="-1" data-micromodal-close>
            <div class="modal__container" role="dialog" aria-modal="true" aria-labelledby="modal-1-title">
              <header class="modal__header">
                <h3 class="modal__title" id="modal-1-title">
                  Share Job
                </h3>
                <button class="modal__close" aria-label="Close modal" data-micromodal-close></button>
              </header>
              <main class="modal__content" id="modal-1-content">
                <p>
                Enter an email address below to share this job to a friend.
                <%= form_tag({controller: "jobs",
                              action: "email_job",
                              job_id: job.id},
                              method: "post") do %>
                <p><%= text_field_tag(:email_address, nil, placeholder: 'Email Address', :job_id => job.id)%></p>
                <p><%= text_area(:message, :text, placeholder: 'Optional Message',  size: "30x10") %></p>
                <p><button class="btn">Send</button></p>
              <% end %>
                </p>
              </main>
            </div>
          </div>
        </div>
     ...

因此,对于每个作业,我都有一个共享链接,当用户单击该链接以“共享”作业时,他们会看到一个显示该表格的模式弹出窗口。我的问题是job.id为每个作业返回相同的作业ID(数据库中的第一个作业)。

2 个答案:

答案 0 :(得分:0)

您真的可以将其设置为普通的嵌套资源并使用虚拟模型:

resources :jobs do
  resources :shares
end
# app/models/share.rb
# This model does not inherit from ActiveRecord::Base and does not have a table
class Share 
  include ActiveModel::Model
  include ActiveModel::Attributes
  attribute :email_adress
  attribute :message
  validates_presence_of :email
end
# shares/_form.html.erb
<%= form_with(model: [job, local_assigns[:share] || Share.new]) do |f| %>
  <p><%= f.text_field(:email_address, placeholder: 'Email Address') %></p>
  <p><%= f.text_area_field(:message, placeholder: "Optional Message",  size: "30x10") %></p>
  <%= f.submit('Send') %>
<% end %>

# shares/new.html.erb
<%= render partial: 'form', job: @job, share: @share %>

仅在验证失败的情况下使用shares/new.html.erb视图。这比尝试重定向用户更容易,然后尝试重新呈现用户所来自的整个视图,并且不那么笨拙。

您可以通过以下方式将表格添加到您的工作索引中:

<% @jobs.each do |job| %>
  <%= render partial: 'shares/form', job: job %>
<% end %>

该控制器只是您的普通嵌套CRUD控制器,只不过您只是调用valid?而不是保存。

class SharesController < ApplicationController
  before_action :set_job
  # POST /jobs/:job_id/shares
  def create
    if @share.valid?
      # @todo send email
      redirect_to @job, success: 'Job shared'
    else
      render :new
    end
  end
  private
  def set_job
    @job = Job.find(params[:job_id])
  end

  def job_params
    params.require(:share).permit(:email_address, :message)
  end
end

答案 1 :(得分:0)

这是一个模态问题,其中模态代码需要唯一的ID。请参阅下面的解决方案。注意,我为ID添加了id="modal-<%=job.id%>"

  <div class="modal micromodal-slide" id="modal-<%=job.id%>" aria-hidden="true">
              <div class="modal__overlay" tabindex="-1" data-micromodal-close>
                <div class="modal__container" role="dialog" aria-modal="true" aria-labelledby="modal-<%=job.id%>-title">
                  <header class="modal__header">
                    <h3 class="modal__title" id="modal-<%=job.id%>-title">
                      Share Job
                    </h3>
                    <button class="modal__close" aria-label="Close modal" data-micromodal-close></button>
                  </header>
                  <main class="modal__content" id="modal-<%=job.id%>-content">
                    <p>
                    Enter an email address below to share this job to a friend.
                    <%= form_tag({controller: "jobs",
                                  action: "email_job",
                                  job_id: job.id},
                                  method: "post") do %>
                    <p><%= text_field_tag(:email_address, nil, placeholder: 'Email Address', :job_id => job.id)%></p>
                    <p><%= text_area(:message, :text, placeholder: 'Optional Message',  size: "30x10") %></p>
                    <p><button class="btn">Send</button></p>
                  <% end %>
                    </p>
                  </main>
                </div>
              </div>
            </div>