【问题标题】:Pre-fill form field with data from prior page in Rails使用 Rails 中前一页的数据预填充表单字段
【发布时间】:2015-03-17 23:56:19
【问题描述】:

我最近将Mailboxer 添加到我的 Rails 应用程序中,并且一切正常。

但是,我希望能够通过单击用户页面上的“向我发送消息”按钮创建新消息,然后在创建表单时已经填写收件人(使用前一页中的用户)消息加载。

有问题的特定字段:

<%= select_tag 'recipients', recipients_options, multiple: true, class: 'new_message_recipients_field' %>

作为参考,recipients_options 在这种情况下可能不会发挥作用,但它是所有用户的列表——我主要在从用户页面以外的任何地方创建消息时使用它——并在我的消息助手中定义

def recipients_options
  s = ''
  User.all.each do |user|
    s << "<option value='#{user.id}'>#{user.first_name} #{user.last_name}</option>"
  end
  s.html_safe
end

还有我的messages_controller.rb

def new
end

def create
  recipients = User.where(id: params['recipients'])
  conversation = current_user.send_message(recipients, params[:message][:body], params[:message][:subject]).conversation
  flash[:success] = "Message has been sent!"
  redirect_to conversation_path(conversation)
end

我已经在每个用户页面上都有指向新消息表单的链接...

&lt;%= link_to 'Message me', new_message_path %&gt;

但我不知道如何在后续表格中预先填写收件人。

有什么建议可以解决这个问题吗?

【问题讨论】:

  • 你为什么使用选择标签?
  • @kendotwill,因为如果发送用户选择,我希望消息能够发送给多个收件人。在我在这个问题中引用的特定用例中,我希望收件人字段预先填写一个选项(我单击了“给我发消息”按钮的用户),但如果发送用户想要添加更多收件人没关系。

标签: javascript ruby-on-rails ruby forms mailboxer


【解决方案1】:

我正在使用 Mailboxer,我可以从列表页面联系卖家。

用户点击列表页面上的联系卖家按钮,在下一页的收件人:表单中填写了卖家的姓名。

我的 Create 方法与您的消息控制器中的相同。但在新方法中我有这个:

MessageController

  def new
    @chosen_recipient = User.find_by(id: params[:to].to_i) if params[:to]
  end

我的列表页面上的联系按钮:

</p>
  <strong>Contact Seller about "<%= @listing.title %>"</strong></br>
  <%= link_to "Send message to #{@listing.user.name}", new_message_path(to: @listing.user.id), class: 'btn btn-default btn-sm' %>
<p>

新的消息表单:

<%= form_tag messages_path, method: :post do %>
  <div class="form-group">
    <%= label_tag 'message[subject]', 'Subject' %>
    <%= text_field_tag 'message[subject]', nil, class: 'form-control', required: true %>
  </div>

  <div class="form-group">
    <%= label_tag 'message[body]', 'Message' %>
    <%= text_area_tag 'message[body]', nil, cols: 3, class: 'form-control', required: true %>
  </div>

  <div class="form-group">
    <%= label_tag 'recipients', 'Choose recipients' %>
    <%= select_tag 'recipients', recipients_options(@chosen_recipient), multiple: true, class: 'form-control chosen-it' %>
  </div>

  <%= submit_tag 'Send', class: 'btn btn-primary' %>
<% end %>

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    link_to 代码更改为:

    <%= link_to 'Message me', new_message_path(recipient_id: @user.id) %>
    

    messages#new 操作中,添加以下代码:

    def new
      @users = User.all
    end
    

    并且,在消息形式中,将 select_tag 替换为:

    <%= select_tag 'recipients', options_from_collection_for_select(@users, :id, :first_name, params[:recipient_id]), multiple: true, class: 'new_message_recipients_field' %>
    

    我对收件人选择进行了一些更改。您可以使用options_from_collection_for_select 方法生成该选项,而不是实现您自己的帮助器。

    参考:http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_from_collection_for_select

    【讨论】:

    • 我试过你的答案,它给了我这个错误:undefined method 'map' for nil:NilClass。我还编辑了我的问题以包括我目前拥有的messages_controller.rb(不包括您建议的更改)。您将如何将您的更改与我目前在 def create 中的内容整合?
    • @Andrew,要使用我在您的观点中建议的更改,请在您的 new 操作中实例化 @users@recipes 实例变量。这应该可以消除错误
    • 将这两行添加到newcreate 确实消除了错误,但仍然没有填充该字段。为什么会这样?您能否编辑您的答案以显示完整的messages_controller.rb,以便我可以确保我按照您的意愿进行操作?
    • 这应该可以。请调试来自用户页面的new 操作。 @recipes 变量应该是包含用户的数组。如果没问题,检查options_from_collection_for_select 的输出,应该是一些选项(每个User 一个)与具有selected 属性的用户对应的选项。
    • 它似乎没有为@recipes 提取任何用户数据...当我尝试在表单页面上简单地打印数据时它只返回#&lt;ActiveRecord::Relation::ActiveRecord_Relation_User:0x007f962d218fe8&gt;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多