gpt4 book ai didi

javascript - 将每个内部的模型传递给 escape_javascript 以呈现表单

转载 作者:行者123 更新时间:2023-11-28 05:51:08 24 4
gpt4 key购买 nike

我无法解决这个问题,我正在使用 simple_form 并且需要使用模型渲染表单,但问题是具有 escape_javascript 的 js 文件无法在每个文件中获取模型

new.html.erb:

<div id="myModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
<%= simple_form_for @team do |t|%>
<div class="row">
<ul class="team_name">
<li><%=t.input :name, label: 'Nombre del Team'%></li>
<%=t.fields_for :checkbox do |c|%>
<li><%=c.input :check, as: :boolean, label: 'Crear y Agregar Usuarios'%></li>
<li><%= t.association :users, label: "Buscar Usuarios"%></li>
<%end%>
</ul>
<%= render :partial => 'teams/forms/team_players_input', :locals =>{ t: t}%>
<div class="modal-footer">
<button class="btn" type="button" id="boton">Cancelar</button>
<%=t.submit "Crear Team", :class => 'btn btn-primary', id: 'create_team_boton'%>
</div>
<%end%>
<%=form_tag team_players_input_teams_path, remote: true, id:'team_players_input', authenticity_token: true do%>

<%end%>
</div>
</div>
</div>

team_players_input controller action:
respond_to do |format|
format.js
end

team_players_input.js.erb:

$("#team-players-input").replaceWith("<%= escape_javascript(render(:partial => 'teams/forms/team_players_input', :locals => {t: t))%>");

_team_players_input.html.erb:

<div class="add_team_user" id="team-players-input">
<%if !@rendering.blank?%>
<%= t.fields_for :user do |u|%>
<ul class="team" id="add_team_players_list">
<div class="player">
<li>Player 1</li>
<ul class="team_player">
<li><%=u.input :name1, label: 'Nombre:'%></li>
<li><%=u.input :lastname1, label: 'Apellido:'%></li>
<li><%=u.input :rut1, label: 'Rut'%></li>
<li><%=u.input :nickname1, label: 'Nick:'%></li>
</ul>
</div>
</ul>
<%end%>
<%end%>

这是我的问题,需要捕获“t”模型才能在渲染文件中使用 fields_for,但会抛出下一个错误

ActionView::Template::Error (wrong number of arguments (0 for 1..2)):
app/views/teams/forms/_team_players_input.html.erb:3:in `_app_views_teams_forms__team_players_input_html_erb___3125175976749038336_69999593801700'
app/views/teams/team_players_input.js.erb:1:in `_app_views_teams_team_players_input_js_erb__1712103055993110209_69999599110300'

我如何将模型传递给渲染文件?请有人帮助我

最佳答案

要访问 team_players_input.js.erb 中的团队对象,您需要确保同一 Controller 中名为 team_players_input 的相应操作方法具有实例对象:@团队。完成此操作后,您可以通过引用 @team 来访问 team_players_input.js.erb 中的 @team 对象。确保使用此 team_player_input 操作方法来呈现 js 请求。

此外,问题是 @team 对象如何在该操作方法中可用?基本上,你会从某个表单/按钮或 View 层的某些内容发出 js 请求。您需要在 js/ajax 请求中传递相关的 team_id 作为参数。稍后使用该 team_id 时,可以通过以下方式提供 @team 对象: @team = Team.find_by_id(params["team_id"].to_i )

编辑1:因此,您应该能够在此远程 js 调用中发送 team_id (可能类似于):

<%=form_tag team_players_input_teams_path, remote: true, id:'team_players_input', team_id: @team.id, authenticity_token: true do%>

然后在对应的action方法中:

team_players_input controller action:
@team = Team.find_by_id(params[:team_id].to_i) # this is to give you rough idea. You need to look at exact params and identity where team_id is in params hash
respond_to do |format|
format.js
end
..

完成这些后,您就可以在 team_players_input.js.erb 中访问 @team:

$("#team-players-input").replaceWith("<%= escape_javascript(render(:partial => 'teams/forms/team_players_input', :locals => {t: @team))%>");

嗯,我想这太冗长了。但是,希望您发现它有帮助:)

关于javascript - 将每个内部的模型传递给 escape_javascript 以呈现表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38069673/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com