【发布时间】:2011-09-03 04:05:32
【问题描述】:
我在一个页面上使用了两个 collection_select 助手。列表本身被正确填充,但是当我提交表单时,NULL 被传递给插入。不知道我在这里做错了什么。更新:添加了控制器代码
新建.html.erb:
<h1>New map_apps_suite</h1>
<%= render 'form' %>
<%= link_to 'Back', map_apps_suites_path %>
表格代码:
<%= form_for(@map_apps_suite) do |f| %>
<% if @map_apps_suite.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@map_apps_suite.errors.count, "error") %> prohibited this map_apps_suite from being saved:</h2>
<ul>
<% @map_apps_suite.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div>
<%= f.label "Application Name:" %>
<%= collection_select(:death_burrito_application, :id, DeathBurritoApplication.all, :id, :death_burrito_name, :prompt => true) %>
</div>
<br>
<br>
<div>
<%= f.label "Project Name:" %>
<%= collection_select(:custom_product_suite, :id, CustomProductSuite.all, :id, :product_suite_name, :prompt => true) %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
日志:
9 月 2 日星期五 20:57:10 开始为 127.0.0.1 发布“/map_apps_suites” -0700 2011 由 MapAppsSuitesController 处理#create as HTML
参数:{"commit"=>"创建地图应用套件", "death_burrito_application"=>{"id"=>"3200"}, "authenticity_token"=>"0PP2U50CScjTbcUdRgbIjkExqo9k3psjlcf4w61ZpqI=", "utf8"=>"✓", "custom_product_suite"=>{"id"=>"1"}} [1m[36mSQL (0.0ms)[0m [1mBEGIN[0m [1m[35mSQL (13.0ms)[0m 描述map_apps_suites[1m[36mAREL (22.0ms)[0m [1mINSERT INTOmap_apps_suites(custom_product_suite_id,death_burrito_application_id) 值 (NULL, NULL)[0m [1m[35mSQL (44.0ms)[0m COMMIT 重定向到 http://localhost:3000/map_apps_suites/3 Completed 302 Found in 185ms
新建控制器代码:
class MapAppsSuitesController < ApplicationController
before_filter :get_apps, :only => [:new, :edit, :destroy, :update]
before_filter :get_suites, :only => [:new, :edit, :destroy, :update]
def get_apps
@applications = DeathBurritoApplication.order(:death_burrito_name).all
end
def get_suites
@custom_prod_suites = CustomProductSuite.order(:product_suite_name).all
end
def new
@map_apps_suite = MapAppsSuite.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @map_apps_suite }
end
end
def create
@map_apps_suite = MapAppsSuite.new(params[:map_apps_suite])
Rails.logger.debug("Params: " + params.inspect)
respond_to do |format|
if @map_apps_suite.save
format.html { redirect_to(@map_apps_suite, :notice => 'Map apps suite was successfully created.') }
format.xml { render :xml => @map_apps_suite, :status => :created, :location => @map_apps_suite }
else
format.html { render :action => "new" }
format.xml { render :xml => @map_apps_suite.errors, :status => :unprocessable_entity }
end
end
end
【问题讨论】:
-
请提供控制器端代码,以便能够回答。
标签: ruby-on-rails