![](/img/trans.png)
[英]Ruby on Rails: Submitting a form referring to another controller via collection_select
[英]Ruby for Rails collection_select not submitting fields to model
我在頁面上使用兩個collection_select助手。 列表本身已正確填充,但是當我提交表單時,會將NULL傳遞給插入。 不知道我在做什么錯。 更新:添加了控制器代碼
New.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 %>
日志:
在2011年9月2日星期五12:07.0.1開始發布POST“ / map_apps_suites”,為127.0.0.1,由MapAppsSuitesController#create處理為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 describemap_apps_suites
[1m [36mAREL(22.0ms) [0m [1mINSERT INTOmap_apps_suites
(custom_product_suite_id
,death_burrito_application_id
)VALUES(NULL,NULL)[0m [1m [35mSQL(44.0ms)[0m COMMIT重定向至http:// localhost:3000 / map_apps_suites / 3已完成302在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
我想你想在你保存custom_product_suite_id
和death_burrito_application_id
您map_apps_suites table
您可以使用以下兩種方法之一保存此文件
1]更改html並保留原樣的控制器代碼
<%= collection_select(:map_apps_suite, :death_burrito_application_id, DeathBurritoApplication.all, :id, :death_burrito_name, :prompt => true) %>
<%= collection_select(:map_apps_suite, :custom_product_suite_id, CustomProductSuite.all, :id, :product_suite_name, :prompt => true) %>
或只需添加f.
到collection_select
<%= f.collection_select(:death_burrito_application_id, DeathBurritoApplication.all, :id, :death_burrito_name, :prompt => true) %>
<%= f.collection_select(:custom_product_suite_id, CustomProductSuite.all, :id, :product_suite_name, :prompt => true) %>
2]更改以下創建方法,並按原樣保留html
@map_apps_suite=MapAppsSuite.new(:custom_product_suite_id=>params[:custom_product_suite][:id],
:death_burrito_application_id => params[:death_burrito_application_id][:id])
我希望這有幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.