在其他“索引视图”中渲染一个模型的“编辑表单”

时间:2022-06-19 00:10:06

I have two models, Teams and Players. On the teams index page I have a list of players that aren't assigned to a team. I'm trying to make a button so that I can click on one of the players with no team and have the 'edit form' of this player show up on the team index page.

我有两个模型,团队和球员。在团队索引页面上,我有一个未分配给团队的玩家列表。我正在尝试制作一个按钮,以便我可以点击其中一个没有团队的玩家,并让该玩家的“编辑形式”显示在团队索引页面上。

This is my current team#index:

这是我目前的团队#index:

= link_to 'New Team', new_team_path
= link_to 'New Player', new_player_path

#teamLists
  - @teams.each do |team|
    .team
      .teamtitle
        .teamname
          = link_to truncate(team.name, length: 18), edit_team_path(team)
        .teammoney
          = number_to_currency(team.adjust_money, precision: 0)
      %table
        %tr.tableheading
          %th.namecolumn Player
          %th.poscolumn Pos
          %th.pricecolumn $
        -team.players.each do |player|
          %tr
            %td.namecolumn= player.name
            %td.poscolumn= player.position
            %td.pricecolumn= player.price
        -(1..(10-team.players.length)).each do |x|
          %tr
            %td ---
=render template: 'players/edit'
=render 'players/playerlist'

and this is my player#edit

这是我的播放器#edit

%h1 Nominated Player
= render 'players/form'

= link_to 'Show', @player
= link_to 'Back', players_path

and the players/form

和球员/形式

  <%= form_for(@player) do |f| %>
     <% if @player.errors.any? %>
      <div id="error_explanation">
        <h2><%= pluralize(@player.errors.count, "error") %> prohibited this player from being saved:</h2>
        <ul>
        <% @player.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
         </ul>
      </div>
    <% end %>

    <div class="field">
      <%= f.label :name %><br />
      <%= f.text_field :name %>
    </div>
    <div class="field">
      <%= f.label :position %><br />
      <%= f.text_field :position %>
    </div>
    <div class="field">
      <%= f.label :price %><br />
      <%= f.number_field :price %>
    </div>
    <div class="field">
      <%= f.label :team_id %><br />
      <%= f.select :team_id, Team.all.map { |team| [team.name, team.id] }, { :include_blank =>     true } %>
    </div>
    <div class="actions">
      <%= f.submit %>
    </div>
  <% end %>

At the moment I get this error 'undefined method `model_name' for NilClass:Class' I think its because the form doesn't have access to @player which is defined in the players edit action. Is there a way I can get this to work somehow?

目前我得到这个错误'未定义的方法`model_name'为NilClass:Class'我认为它是因为表单无法访问在玩家编辑操作中定义的@player。有没有办法让我以某种方式工作?

1 个解决方案

#1


1  

You can reference any partial from another view page, and that's fine. However, like in your case, if that partial you require needs some instance variables (like @player) you'll have to either: A) declare it in the controller of Teams, or B) pass it in to the partial.

您可以从另一个视图页面引用任何部分,这没关系。但是,就像你的情况一样,如果你需要的部分需要一些实例变量(比如@player),你必须要么:A)在团队的控制器中声明它,或者B)将它传递给部分。

So for A), in your Teams controller for action index, just add @player = Player.new or whatever you need it to be.

所以对于A),在你的Teams控制器动作索引中,只需添加@player = Player.new或者你需要的任何东西。

For B), do:

B),做:

render :partial => "my_partial", :locals => {:player => Player.new}

render:partial =>“my_partial”,:locals => {:player => Player.new}

#1


1  

You can reference any partial from another view page, and that's fine. However, like in your case, if that partial you require needs some instance variables (like @player) you'll have to either: A) declare it in the controller of Teams, or B) pass it in to the partial.

您可以从另一个视图页面引用任何部分,这没关系。但是,就像你的情况一样,如果你需要的部分需要一些实例变量(比如@player),你必须要么:A)在团队的控制器中声明它,或者B)将它传递给部分。

So for A), in your Teams controller for action index, just add @player = Player.new or whatever you need it to be.

所以对于A),在你的Teams控制器动作索引中,只需添加@player = Player.new或者你需要的任何东西。

For B), do:

B),做:

render :partial => "my_partial", :locals => {:player => Player.new}

render:partial =>“my_partial”,:locals => {:player => Player.new}