将每个模型中的模型传递给escape_javascript以呈现表单

时间:2016-06-28 07:25:53

标签: javascript ruby-on-rails model render simple-form

我无法解决这个问题,我正在使用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'

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

1 个答案:

答案 0 :(得分:0)

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

此外,问题是如何在该行动方法中提供@team对象?基本上,您可以从某个表单/按钮或视图层中的某些内容发出js请求。您需要在js / ajax请求中传递相关的team_id as参数。稍后使用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%>

然后在相应的动作方法中:

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中的team_players_input.js.erb

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

嗯,我猜它太冗长了。但是,希望你觉得它有用:)