我确信我的问题必须有一个非常简单的解决方案,但我不知道为什么我似乎不能把手指放在上面,问题是我正在尝试提交表单,但每次提交表单时,创建操作控制器都会给出以下错误
提交表单后,错误所说的 null 的所有这些值是如何通过参数传递的,Mysql2::错误:列"preffered_players"不能为空:插入到
)saved_sessions
(preffered_players
,session_id
,preffered_opponents
,game_id
,game_type
,opponents_list
,banned_players
,players_list
)值(空,空
任何人都可以告诉我如何使用我从表单中获得的参数值来填写查询中的小 null,创建控制器如下
def create
opponents_list = params[:opponents_list]
banned_players = params[:banned_players]
game_type = params[:game_type]
session_id = params[:session_id]
preffered_opponents = params[:preffered_opponents]
game_id = params[:game_id]
players_list = params[:players_list]
preffered_players = params[:preffered_players]
@saved_sessions = SavedSession.new(params[:saved_sessions])
respond_to do |format|
if @saved_sessions.save
format.html { redirect_to(@game, :notice => 'Game was successfully created.') }
format.xml { render :xml => @game, :status => :created, :location => @game }
else
format.html { render :action => "new" }
format.xml { render :xml => @game.errors, :status => :unprocessable_entity }
end
end
end
感谢您的任何建议...
我包含表单代码,但它非常混乱(这是一个巨大的表单)加上它的 Haml :)
- form_tag('/saved_sessions',:method => :post, :id => "new_game") do
.select_option
.list_col1
.fl
%img{:alt => "image", :border => "0", :src => "/images/happy_face.png"}/
.blue_col
= text_field_tag :preffered_opponents, "", :class => :blue_bar
.clr
.list_col2
.list_col2_top_row
.radio_row
.fl
%input{:type => "radio", :name => "battle_scale", :value => "3"}
.fl 3 vs 3
.fl
%input{:type => "radio", :name => "battle_scale", :value => "5"}
.fl 5 vs 5
.clr
%div
.fl
%img{:alt => "image", :border => "0", :src => "/images/sad_face.png"}/
.fl
/%input#banned_players.white_bar{:type => "text"}/
= text_field_tag :banned_players, "", :class => :white_bar
.clr
.versus_row
#inputs-vs.versus_col1
.versus_col_row
.blue_col
/%input#input-1-vs.blue_bar{:type => "text"}/
= text_field_tag 'input-1-vs', "", :class => :blue_bar
.fl
%img{:alt => "image", :border => "0", :src => "/images/lock.png"}/
.clr
.versus_col_row
.blue_col
/%input#input-2-vs.blue_bar{:type => "text"}/
= text_field_tag 'input-2-vs', "", :class => :blue_bar
.fl
%img{:alt => "image", :border => "0", :src => "/images/lock.png"}/
.clr
.versus_col_row
.blue_col
/%input#input-3-vs.blue_bar{:type => "text"}/
= text_field_tag 'input-3-vs', "", :class => :blue_bar
.fl
%img{:alt => "image", :border => "0", :src => "/images/lock.png"}/
.clr
.versus_col_row
.blue_col
/%input#input-4-vs.blue_bar{:type => "text"}/
= text_field_tag 'input-4-vs', "", :class => :blue_bar
.fl
%img{:alt => "image", :border => "0", :src => "/images/lock.png"}/
.clr
.versus_col_row
.blue_col
/%input#input-5-vs.blue_bar{:type => "text"}/
= text_field_tag 'input-5-vs', "", :class => :blue_bar
.fl
%img{:alt => "image", :border => "0", :src => "/images/lock.png"}/
.clr
.versus_col2
%img{:alt => "0", :border => "0", :src => "/images/vs.png"}/
#inputs.versus_col1
.versus_col_row
.fl
%img{:alt => "image", :border => "0", :src => "/images/lock.png"}/
.blue_col
/%input#input-1.pink_bar{:name => "blah", :type => "text"}/
= text_field_tag 'input-1', "", :class => :pink_bar
.clr
.versus_col_row
.fl
%img{:alt => "image", :border => "0", :src => "/images/lock.png"}/
.blue_col
/%input#input-2.pink_bar{:name => "blah", :type => "text"}/
= text_field_tag 'input-2', "", :class => :pink_bar
.clr
.versus_col_row
.fl
%img{:alt => "image", :border => "0", :src => "/images/lock.png"}/
.blue_col
/%input#input-3.pink_bar{:name => "blah", :type => "text"}/
= text_field_tag 'input-3', "", :class => :pink_bar
.clr
.versus_col_row
.fl
%img{:alt => "image", :border => "0", :src => "/images/lock.png"}/
.blue_col
/%input#input-4.pink_bar{:name => "blah", :type => "text"}/
= text_field_tag 'input-4', "", :class => :pink_bar
.clr
.versus_col_row
.fl
%img{:alt => "image", :border => "0", :src => "/images/lock.png"}/
.blue_col
/%input#input-5.pink_bar{:name => "blah", :type => "text"}/
= text_field_tag 'input-5', "", :class => :pink_bar
.clr
.clr
.session_row
%p
%b You can save and share this session by entering your email. A unique code will be sent
%p.small_text (Session are stored for 30 days)
.seesion_send_row
.fl
/%input.big_white_bar{:type => "text"}/
= text_field_tag 'email_bar', "", :class => :big_white_bar
.fl
%span 2+2=
%span
%span
/%input.small_bar{:type => "text"}/
= text_field_tag 'code_bar', "", :class => :small_bar
.fl
/%a{:href => "#."}
/%img{:alt => "image", :border => "0", :src => "/images/send_button.png"}/
= submit_tag("Send")
希望这可以帮助你们更好地了解问题。
你似乎从两个不同的地方拉来。方法顶部的代码块从传入参数的顶层提取值,但稍后使用 params[:saved_sessions] 输入初始化新的 SavedSession。这两件事似乎不匹配...
如果在第一行的 opponents_list = params[:opponents_list] 处找到 saved_sessions.Rivals_list,那么当你稍后尝试保存 params[:saved_sessions] 时,你从参数中的不同位置获取它......
编辑:因此您不需要操作中的两组值。 如果您像这样构建表单(或至少像这样),您将正确获取参数,并且可以盲目地将它们发送到 SavedSession.new,然后调用 .save。
- form_for(@aved_session) do |f|
-.blue_col
= f.text_field :preffered_opponents, "", :class => :blue_bar
如果你允许f.text_field构建id和name,那么rails magic将确保它们被正确命名/id'd,然后更多的rails magic解释它们并正确构建你的输入参数。这会将您的参数构建为:
params = {:saved_session => {:preferred_opponents => "text value" }...
然后你可以把这些参数拉出来:
saved_session_input = params[:saved_session]
并像您一样将它们传递给新的。 不同之处在于,现在这些参数实际上在那里。
你拥有它的方式是这样的:
params = {:preferred_opponents => "text value", :saved_session => {:preferred_opponents => nil }...
这意味着您正在为新的预期值传递 nil。请注意,在调用 save 之前,您实际上并没有将本地值分配给新saved_session。
我希望这更清楚。