小编Mus*_*hir的帖子

尝试在Rails中保存嵌套属性时的ActiveRecord :: AssociationTypeMismatch

我阅读了很多关于`has_one关系和嵌套属性的页面,但是没有成功地完成这项工作.任何帮助都会很棒.

每个用户都有_one网络.我正在尝试以一种形式收集两个属性的信息,但不断获得异常ActiveRecord::AssociationTypeMismatch in UsersController#create

传递的参数是:

{"utf8"=>"✓",
 "authenticity_token"=>"I54tm1ovzHEHaXbBLTT+5tqBJv2795sKg978ot3HDBc=",
 "user"=>{"name"=>"Bilbo Baggins",
 "email"=>"bilbo@lotr.com",
 "password"=>"[FILTERED]",
 "password_confirmation"=>"[FILTERED]",
 "network"=>{"home_lng"=>"-87.91894912719727",
 "home_lat"=>"43.03812464542969",
 "center_lng"=>"-87.91894912719727",
 "center_lat"=>"43.03812464542969",
 "radius"=>"500"}},
 "commit"=>"Sign up"}
Run Code Online (Sandbox Code Playgroud)

我猜测参数Network必须以某种方式出现,network_attributes但我不知道如何.

控制器:

def create
    @user = User.new(params[:user])
    if (@user.save)
      sign_in @user
      flash[:success] = "One ring to rule them all!"
      redirect_to @user
    else
      @title = "The journey begins..."
      render 'new'
    end
end
Run Code Online (Sandbox Code Playgroud)

视图:

<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.text_field :email %><br />
<%= f.label :password %>
<%= f.password_field :password …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails nested-forms nested-attributes ruby-on-rails-3

4
推荐指数
1
解决办法
8064
查看次数

点击输入不会以HTML格式调用javascript函数

我有一个带有一个输入字段的简单表单.它在我使用提交时工作正常,但是在文本字段中按Enter键会在URL中重新加载带有表单变量的页面.

我查看了在线提供的许多解决方案(除了JQuery,因为它看起来像是一些过于简单的东西)并且无法让'enter'工作.任何帮助都会很棒

<form name="myform" action="" method="GET" > Enter text: <br>
  <input type="text" name="queryinput" onkeyup="if(isEnterPressed(event)){initialize(this.form.queryinput.value);}"><P>
  <input type="button" name="Search" value="Search" onClick="initialize(this.form.queryinput.value);">
  <input type="submit" value="Reset" onclick="reset();" />
</form>

function isEnterPressed(e){
  var keycode=null;
  if (e!=null){
    if (window.event!=undefined)    
      if (window.event.keyCode) keycode = window.event.keyCode; 
      else if (window.event.charCode) keycode = window.event.charCode;
    }else{
            keycode = e.keyCode;
    }
  }
return (keycode == 13);}
Run Code Online (Sandbox Code Playgroud)

使用onsubmit编辑1:版本而不是键码侦听器:

<form name="myform" action="" onsubmit="return initialize(this.form.queryinput.value)"  method="GET"> Enter text:<br>
<input type="text" name="queryinput">
<input type="submit" value="submit" >
<input type="button" value="Reset" onclick="clearAllPoints();"/>
</form>
Run Code Online (Sandbox Code Playgroud)

使用onSubmit会导致单击按钮的行为与按Enter键相同,但两个版本都不起作用.

javascript forms

1
推荐指数
1
解决办法
3127
查看次数