小编Ala*_*n H的帖子

rails中嵌套的强参数 - 期望的AssociationTypeMismatch MYMODEL,得到了ActionController :: Parameters()

我正在渲染一个模型,它是JSON中的子书籍,如下所示:

{"id":2,"complete":false,"private":false, "books" [{ "id":2,"name":"Some Book"},.....
Run Code Online (Sandbox Code Playgroud)

然后我通过将相同的JSON传递回我的控制器来更新此模型,我收到以下错误:

ActiveRecord :: AssociationTypeMismatch(书(#2245089560)预期,得到ActionController ::参数(#2153445460))

在我的控制器中,我使用以下内容进行更新:

@project.update_attributes!(project_params)

private

def project_params
    params.permit(:id, { books: [:id] } )
end
Run Code Online (Sandbox Code Playgroud)

无论我将哪些属性列入白名单,permit我似乎无法保存孩子模型.

我错过了一些明显的东西吗

更新 - 另一个例子:

控制器:

def create
    @model = Model.new(model_params)
end
def model_params
    params.fetch(:model, {}).permit(:child_model => [:name, :other])
end
Run Code Online (Sandbox Code Playgroud)

请求:

post 'api.address/model', :model => { :child_model => { :name => "some name" } }
Run Code Online (Sandbox Code Playgroud)

模型:

accepts_nested_attributes_for :child_model
Run Code Online (Sandbox Code Playgroud)

错误:

期望的ChildModel,得到了ActionController :: Parameters

尝试这种方法无济于事:http://www.rubyexperiments.com/using-strong-parameters-with-nested-forms/

json strong-parameters ruby-on-rails-4

11
推荐指数
3
解决办法
9009
查看次数

无法找到'facebook'动作 - devise/omniauth

我正在尝试按照本指南在我的应用程序中实现facebook身份验证

在Facebook上授权后,我得到以下内容:

Unknown action

The action 'facebook' could not be found for Devise::OmniauthCallbacksController
Run Code Online (Sandbox Code Playgroud)

我已经在app/controller/users/omniauth_callbacks_controller.rb中实现了这个方法:

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def facebook
    # You need to implement the method below in your model (e.g. app/models/user.rb)
    @user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)

    if @user.persisted?
      sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
      set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
    else
      session["devise.facebook_data"] = request.env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

我的回调路线是:

devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" } …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails devise omniauth ruby-on-rails-3

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

ActiveRecord创建without_protection - 错误的参数个数(2个为1)

我无法弄清楚我的User模型上的这个create语句出了什么问题:

User.create({first_name: "Alan",....}, :without_protection => true)
Run Code Online (Sandbox Code Playgroud)

给我堆栈跟踪:

ArgumentError: wrong number of arguments (2 for 1)
from /Users/alanheppenstall/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-4.0.0/lib/active_record/persistence.rb:32:in `create'
from (irb):56
from /Users/alanheppenstall/.rvm/gems/ruby-1.9.3-p362/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
from /Users/alanheppenstall/.rvm/gems/ruby-1.9.3-p362/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
from /Users/alanheppenstall/.rvm/gems/ruby-1.9.3-p362/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
Run Code Online (Sandbox Code Playgroud)

我的语法匹配:http://apidock.com/rails/ActiveRecord/Base/create/class/

我正在使用这个模型设计 - 这可能是个问题吗?我找不到他们覆盖initialize()的任何地方.

谢谢!

activerecord ruby-on-rails-4

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