1

仍在继续我的第一个 Rails 程序(ruby 2.0,rails 4.0)。我有一个下拉菜单,显示来自第二个模型的静态选项,但是当我单击该下拉菜单中的选择时,什么也没有发生。将其保存到记录中(关闭 presence_of 验证)只会将该字段留空。

我正在考虑控制器或视图中的某些内容?有什么帮助吗?

对不起,大量的代码转储,但我很菜鸟,我不确定什么是相关的

联系方式.rb

class Contact < ActiveRecord::Base

attr_accessible :first_name, :last_name, :email, :zip_code, :favorite_color, :color_id, :name, :hex
belongs_to :color
accepts_nested_attributes_for :color

颜色.rb

class Color < ActiveRecord::Base
  has_many :contacts
  accepts_nested_attributes_for :contacts
  attr_accessible :name, :hex
end

联系人控制器.rb

class ContactsController < ApplicationController
before_action :set_contact, only: [:show, :edit, :update, :destroy]
before_filter :prepare_colors

# GET /contacts
# GET /contacts.json
def index
  @contacts = Contact.all
end

# GET /contacts/1
# GET /contacts/1.json
def show
end

# GET /contacts/new
def new
  @contact = Contact.new
end

# GET /contacts/1/edit
def edit
end

# POST /contacts
# POST /contacts.json
def create
  @contact = Contact.new(contact_params)
  respond_to do |format|
    if @contact.save
      format.html { redirect_to @contact, notice: 'Contact was successfully created.' }
      format.json { render action: 'show', status: :created, location: @contact }
    else
      format.html { render action: 'new' }
      format.json { render json: @contact.errors, status: :unprocessable_entity }
    end
  end
end

# PATCH/PUT /contacts/1
# PATCH/PUT /contacts/1.json
def update
  respond_to do |format|
    if @contact.update(contact_params)
      format.html { redirect_to @contact, notice: 'Contact was successfully updated.' }
      format.json { head :no_content }
    else
      format.html { render action: 'edit' }
      format.json { render json: @contact.errors, status: :unprocessable_entity }
    end
  end
end

# DELETE /contacts/1
# DELETE /contacts/1.json
def destroy
  @contact.destroy
  respond_to do |format|
    format.html { redirect_to contacts_url }
    format.json { head :no_content }
  end
end

private
  # Use callbacks to share common setup or constraints between actions.
  def set_contact
    @contact = Contact.find(params[:id])
  end

  def prepare_colors
    @colors = Color.all
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def contact_params
    params.require(:contact).permit(:first_name, :last_name, :email, :zip_code, :favorite_color, :color_id)
  end

结尾

意见/联系人/_form.html.erb

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

      <ul>
      <% @contact.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :first_name %><br>
    <%= f.text_field :first_name %>
  </div>
  <div class="field">
    <%= f.label :last_name %><br>
    <%= f.text_field :last_name %>
  </div>
  <div class="field">
    <%= f.label :email %><br>
    <%= f.text_field :email %>
  </div>
  <div class="field">
    <%= f.label :zip_code %><br>
    <%= f.number_field :zip_code %>
  </div>
  <div class="field">
    <%= f.label :favorite_color %><br>
    <%= f.collection_select(:color_id, @colors, :id, :name, :include_blank => "Please select") %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

和 /views/contacts/index.html

h3>Listing contacts</h1>

<table>
  <thead>
    <tr>
      <th>First name</th>
      <th>Last name</th>
      <th>Email</th>
      <th>Zip code</th>
      <th>Favorite color</th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>

  <tbody>
    <% @contacts.each do |contact| %>
      <tr>
        <td><%= contact.first_name %></td>
        <td><%= contact.last_name %></td>
        <td><%= contact.email %></td>
        <td><%= contact.zip_code %></td>
        <td><%= contact.favorite_color %></td>
        <td><%= link_to 'Show', contact %></td>
        <td><%= link_to 'Edit', edit_contact_path(contact) %></td>
        <td><%= link_to 'Destroy', contact, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New Contact', new_contact_path %>
4

2 回答 2

0

强参数

首先,如果您使用的是 rails 4.0,则不需要此行:

attr_accessible :first_name, :last_name, :email, :zip_code, :favorite_color, :color_id, :name, :hex

这已经被strong_paramsnow 替换了,所以你应该把它放在你的联系人控制器的底部:

private
def contact_params
    params.require(:contact).permit(:first_name, :last_name, :email, :zip_code, :favorite_color, :color_id, :name, :hex, colors_attributes: [])
end

Fields_For

接下来是您需要fields_for在表单中包含标签,如下所示:

<%= f.fields_for :colors do |color| %><br>
    <%= f.collection_select(:color_id, @colors, :id, :name, :include_blank => "Please select") %>
<% end %>

我认为这会奏效。您的代码很长,所以我想如果您在实现它时遇到问题,请发表评论:)


更新

您需要将此添加到您的新功能中:

# GET /contacts/new
def new
  @contact = Contact.new
  @contact.color.build

  @colors = Color.all
end

另外,我不知道这是否会再次起作用,但您可能需要编辑您的 collection_select 以获得此代码:

<%= f.fields_for :colors do |color| %><br>
    <%= color.collection_select(:color, @colors, :id, :name, :include_blank => "Please select") %>
<% end %>

最后,你应该在你的nested_attributes 中包含 :id 属性:

private
def contact_params
    params.require(:contact).permit(:first_name, :last_name, :email, :zip_code, :favorite_color, :color_id, :name, :hex, colors_attributes: [])
end
于 2013-10-13T21:47:19.963 回答
0

好的,我不完全确定这里的关系是什么,但在我看来,您的联系人模型中有两列是出于相同的原因创建的:favorite_color 和 colour_id。您没有设置 favorite_color 值,但您正在尝试显示它。我猜 color_id 将成为联系人最喜欢的颜色的 foreign_key。如果是这样,您需要重命名关联:

belongs_to :favorite_color, class_name: 'Color'

将 color_id 列重命名为 favorite_color_id,然后在您的视图中:

# form
<div class="field">
  <%= f.label :favorite_color %><br>
  <%= f.collection_select(:favorite_color_id, @colors, :id, :name, :include_blank => "Please select") %>
</div>

# index

<td><%= contact.favorite_color.name %></td>
于 2013-10-13T22:03:36.970 回答