【问题标题】:Wrong number of arguments (2 for 1) error in M.Hartl's Rails Tutorial 7.4.1M.Hartl 的 Rails 教程 7.4.1 中的参数数量错误(2 比 1)错误
【发布时间】:2014-10-19 17:29:29
【问题描述】:

我一直在关注最新版本的 M.Hartl 的 Rails 教程,但在第 7.4.1 章遇到了问题。我创建了一个注册表单,它与用户控制器的新操作相关联。当表单使用有效信息提交时,控制器应重定向到新用户配置文件,但是我会出现以下错误...

我已经包含了 routes.rb 代码以及用户控制器代码。有人可以帮我吗?

当我访问 url ../users/1 页面实际上呈现我的用户,所以我知道用户已经创建并保存到数据库中。不知道是不是redirect_to方法的实现出错了?

任何帮助将不胜感激!


UsersController#create 中的参数错误 参数数量错误(2 比 1)

提取的源代码(第 19 行附近):

private
  def _compute_redirect_to_location_with_xhr_referer(options)
    store_for_turbolinks begin
      if options == :back && request.headers["X-XHR-Referer"]
        _compute_redirect_to_location_without_xhr_referer(request.headers["X-XHR-Referer"])

用户控制器:

class UsersController < ApplicationController

  def new
    @user = User.new
  end

  def show
    @user = User.find(params[:id])
  end

  def create
    @user = User.new(user_params)  
    if @user.save
      flash[:success] = "Welcome to the Sample App!"
      redirect_to @user
    else
      render 'new'
    end
  end

  private 

    def user_params
      params.require(:user).permit(:name, :email, :password, :password_confirmation)
    end
end

Routes.rb:

Rails.application.routes.draw do
  root             'static_pages#home'
  get 'help'    => 'static_pages#help'
  get 'about'   => 'static_pages#about'
  get 'contact' => 'static_pages#contact'
  get 'signup'  => 'users#new'
  resources :users
end

【问题讨论】:

  • 看起来类似于this
  • 你能把错误页面中的params内容贴出来吗?
  • 使用堆栈跟踪发布完整错误

标签: ruby-on-rails ruby railstutorial.org


【解决方案1】:

您的 turbolinks 版本似乎很旧。尝试更新到最新版本:

gem 'turbolinks', '~&gt; 2.3.0'

【讨论】:

  • @StevanPopovic ,你应该点击链接“接受”这个答案。
  • @lugolabs 你能解释一下为什么turbolinks版本修复了这个问题吗?
  • 当您的 Gemfile 中有特定版本的 turbolinks 时会发生这种情况,但安装的 Rails 版本需要另一个版本。例如。当您手动升级 Rails 版本时。
猜你喜欢
  • 1970-01-01
  • 2012-04-15
  • 2015-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-24
  • 2016-05-22
相关资源
最近更新 更多