简体   繁体   中英

Pass model inside each to escape_javascript to render a form

i can't solve this problem, i'm using simple_form and need to render a form with a model, but the problem is the js file that have a escape_javascript doesn't get the model inside each

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%>

There is my problem, need to catch the "t" model to use fields_for in the render file, but throw me the next error

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'

How i can pass the model to render file? please somebody help me

For accessing team object in your team_players_input.js.erb , you need to make sure that corresponding action method named team_players_input in the same controller has an instance object: @team . Once this is done, you can access the @team object in team_players_input.js.erb by making reference to @team . Make sure that this team_player_input action method is made to render js request.

Further, the question comes that how will @team object be available in that action method? Basically, you would me making a js request from some form / button or something from the views layer. You need to pass the relevant team_id as parameter in the js/ajax request. And later on using that team_id , @team object can be made available by something like: @team = Team.find_by_id(params["team_id"].to_i)

EDIT 1: So, you should be able to send team_id in this remote js call (probably like):

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

And then in the corresponding action method:

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
  ..

Once this much is done, then you can access @team in the team_players_input.js.erb :

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

Well, I guess it's too verbose. But, hope you find it helpful : )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM