Rails从外部模型的动态下拉中节省价值不起作用

时间:2017-04-28 21:21:07

标签: ruby-on-rails ruby simple-form nested-attributes

我有两种模式:

class Location < ActiveRecord::Base
    has_many :cows
    accepts_nested_attributes_for :cows
end

class Cow < ActiveRecord::Base
    belongs_to :location
end

每个位置都有一个ID和一个名字。一头奶牛由一个id,两个字符串和两个日期组成,它还包含一个location_id。

在我的牛视图中,我有一个每个位置的下拉列表。每当我创建,编辑或删除位置时,它都会自动更新。当我从下拉列表中选择一个位置时,奶牛指数会根据奶牛中的拟合location_id而变化。

Cows index.html.erb:

<!-- Dropdown Button -->
<a class='dropdown-button' href='#' data-activates='dropdown_locations'>
<i class="fa fa-arrow-down" aria-hidden="true"></i> Ort auswählen</a>

<!-- Dropdown Content -->
<ul id='dropdown_locations' class='dropdown-content'>
  <% @locations_all.each do |loc| %>
    <li><%= link_to loc.name, cows_path(location: loc.id) %></li>
  <% end %>
</ul>

cows索引页面是一个用于显示内容并同时添加新值的页面。正如我所提到的,索引会根据所选位置而变化。但它只有在我在数据库编辑器中手动填充牛表时才有效,因为从下拉列表中选择的位置不会保存到奶牛。

以下是在牛群中创建新奶牛的表格index.html.erb:

<%= simple_form_for Cow.new do |f| %>
    <%= f.input :ohrmarke, as: :string, input_html: { maxlength: 5 } %>
    <%= f.input :stallnummer, as: :string, input_html: { maxlength: 3 } %>
    <%= f.hidden_field :location_id, value: @location_id %>

    <button class="btn waves-effect waves-light" type="submit" name="action">Zuordnung speichern</button>
<% end %>

我使用hidden_​​field作为location_id,值应该是来自保管箱的所选位置的location_id。

但由于某些原因,创造新的牛只是行不通。这是我的牛控制器(缩短):

def index
  @locations_all = Location.all
  if (params[:location] && cows = Cow.where(location_id: params[:location]))
    @cows = cows
    @location = Location.where(id: params[:location])
  else
    @cows = Cow.all
  end
end

def new
  @cow = Cow.new
end

def create
  @cow = Cow.new(cow_params)

  respond_to do |format|
    if @cow.save
      # format.html blabla... the usual stuff
    end
  end
end

private
  def set_cow
    @cow = Cow.find(params[:id])
  end

  def cow_params
    params.require(:cow).permit(:ohrmarke, :hin, :weg, :stallnummer, location_attributes: [:location_id])
  end
end

我认为我的hidden_​​field或创建控制器无效。但我不知道自己做错了什么。至少根据所选位置更改索引有效。感谢您提前提供任何帮助。

编辑:我在你的帮助下解决了这个问题,并且我自己解决了这个问题。

3 个答案:

答案 0 :(得分:1)

主要问题是您说过Location accepts_nested_attributes_for for :cows,但您的表单和控制器设置为Cow accepts_nested_attributes_for :location.我认为如果您将模型更改为

class Location < ActiveRecord::Base
has_many :cows

end

class Cow < ActiveRecord::Base
    belongs_to :location
    accepts_nested_attributes_for :location
end
你应该去某个地方。

答案 1 :(得分:1)

我认为你没有设置位置

def cow_params
  params.require(:cow).permit(:ohrmarke, :hin, :weg, :stallnummer, location_attributes: [:location_id])
end

应该是

def cow_params
  params.require(:cow).permit(:ohrmarke, :hin, :weg, :stallnummer, :location_id)
end

该位置没有location_id。我想你只想把它放在牛身上。

答案 2 :(得分:0)

经过一些建议我改变了代码并设置了location_id。现在一切正常。这是新代码:

牛控制器:

def index
  @locations_all = Location.all
  if (params[:location] && cows = Cow.where(location_id: params[:location]))
    @cows = cows
    @location_id = params[:location] # used for the form
    @location = Location.find(@location_id) # used for ex. showing the name
  else
    @cows = Cow.all
  end
end

def cow_params
  params.require(:cow).permit(:ohrmarke, :hin, :weg, :stallnummer, :location_id)
end

形式:

<%= f.hidden_field :location_id, value: @location_id %>

我也相应地更改了模型(将nested_attributes移动到了Cow)。