首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从Rails中相同视图中的simple_form中获取值

如何从Rails中相同视图中的simple_form中获取值
EN

Stack Overflow用户
提问于 2022-08-16 02:33:09
回答 1查看 42关注 0票数 -2

我需要从f.association中获得一个simple_form形式的值。这将从控制器获得它的值,并传输所有的房间,我需要这个值来加载引导5模式,我需要重新加载所选房间的信息。

模型工作,但我需要占用set_parms,以获得数据。

你能给我点主意或者怎么做吗?

这是我现有的代码:

表格

代码语言:javascript
运行
复制
<%= simple_form_for [@reservation, @item_reservation] do |f| %>
  <%= f.association :room, collection: @room.map{|r| [r.name, r.id]} %>
  <%= f.submit "Add" %>
  <button type="button" class="btn " data-bs-toggle="modal" data-bs-target="#exampleModal">
  Launch demo modal
</button>
  <%= link_to "Back", client_reservations_path(@reservation.client_id) %>
<% end %>

视图

代码语言:javascript
运行
复制
<h1>ItemReservations#new</h1>
<p>Find me in app/views/item_reservations/new.html.erb</p>
<td><%= link_to "Follow", new_reservation_user_reservation_path(@reservation)%> </td>

<%=render "form", item_reservation: @item_reservation %>
<%= link_to "Back", client_reservations_path(@reservation.client_id) %>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

<br/>
<h2>Rooms</h2>
<div class="text-end">
  <small><%= @message %></small>
</div>
<br/>
<table id="Room_select" class="table table-striped table-sm">
  
  <head>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-16 13:06:53

假设:

-你做了:

  1. 带有预订表单的页面加载
  2. 用户从选择字段中选择一个房间。
  3. 用户单击“启动演示模式”按钮

-你必须做:

  1. 房间数据应添加到模态中。
  2. 模态应该打开

如果这一切都是正确的,那么您有几个选择:

  1. 在这种情况下,Rails 7附带了霍特维尔
  2. 如果您不想学习Turbo,可以使用Rails AJAX和UJS进行更早版本的Rails。
  3. 使用没有Rails的Javascript和AJAX

使用这些选项中的任何一个,您本质上将:

  1. 从select字段获取当前选定的选项
  2. 将该选项远程发送到自定义Rails控制器(例如RoomsController#modal_show) (使用AJAX或Turbo (Hotwire))
  3. 控制器将加载房间数据,并返回一些Javascript以执行(在Hotwire的情况下,HTML将呈现)
  4. 返回的Javascript将把房间数据放到模型中,并触发打开模型。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73368192

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档