简体   繁体   English

redirect_to的参数数目错误(0..1为2)

[英]wrong number of arguments (2 for 0..1) at redirect_to

Error image 错误图片

argument error 参数错误

home_controller.rb home_controller.rb

class HomeController < ApplicationController

def home
 @activities = Activity.order("created_at DESC").page(params[:page]).per_page(5)
end

def login
  render 
end

def logout
  session[:school_id] = nil
  session[:parent_id] = nil
  session[:user_id] = nil

  redirect_to :controller => 'home', :action => 'login'
end

Gemfile 的Gemfile

source 'https://rubygems.org'
gem 'rails', '4.2.4'
gem 'mysql2', '~> 0.3.20'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'remotipart', '~> 1.2'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'bootstrap-sass', '~> 3.3.5'
gem 'sass-rails', '~> 5.0'
gem 'bootstrap_form'
gem 'devise', '~> 3.5', '>= 3.5.2'
gem 'bartt-ssl_requirement', '~>1.4.0', :require => 'ssl_requirement'
gem 'will_paginate'
gem 'paperclip'
gem 'bcrypt', '~> 3.1.7'
group :development, :test do
 console
  gem 'byebug'
end
redirect_to :controller => 'home', :action => 'login'

This is not correct, not to mention ugly. 这是不正确的,更不用说丑陋了。 Instead, create a route in routes.rb that does that action. 而是在routes.rb中创建一个执行该操作的路由。 For example: 例如:

get 'login' => 'home#login'

Then you would use: 然后,您将使用:

redirect_to login_path

BONUS 奖金

****You should read up on rails routing to make better routes too.**** ****您还应该阅读Rails路线,以使路线更好。

Also, a home controller that handles logins does not seem very clean. 另外,处理登录的家庭控制器看起来也不是很干净。

A better route would be something like the following but it requires different controller setup: 更好的路由如下所示,但需要不同的控制器设置:

resources :sessions

which creates the following route and many others(not exactly but lets ignore the details): 这会创建以下路线和许多其他路线(不完全是,但可以忽略详细信息):

get 'login' => 'sessions#new'

Again, I recommend you read up on Rails routing . 再次,我建议您阅读Rails routing

It looks to me that you may have had your code written incorrectly initially, and even though you've fixed the source code, Rails is still using the old (incorrect) version. 在我看来,您最初可能编写的代码不正确,即使您已修复源代码,Rails仍在使用旧的(不正确的)版本。

You must have had some variant of: 您必须具有以下一些变体:

redirect_to 'home', :action => 'login'
redirect_to 'home', 'login'

before. 之前。

Leave your code the way you have it ( redirect_to(:controller => 'home', :action => 'login') ), and make sure you restart your Rails process correctly. 保持代码的原样( redirect_to(:controller => 'home', :action => 'login') ),并确保正确重新启动Rails进程。

只是为了解释redirect_to实际上期望的是路径而不是控制器或动作,Helsing非常漂亮地回答了如何从控制器及其动作中做出路由。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ArgumentError(参数数量错误(0..1为3)): - ArgumentError (wrong number of arguments (3 for 0..1)): Rails-突然,redirect_to总是给错误错误的参数数量(0代表1) - Rails - Suddenly, redirect_to always gives error wrong number of arguments (0 for 1) Geocoder:ArgumentError:参数数目错误(0..1为3) - Geocoder : ArgumentError: wrong number of arguments (3 for 0..1) Rails - 错误的参数数量(2为0..1)错误 - Rails - wrong number of arguments (2 for 0..1) error ArgumentError(arguments 的编号错误(给定 2,预期 0..1) - ArgumentError (wrong number of arguments (given 2, expected 0..1) 在Rails 4.2中使用redirect_to时,为什么有错误的参数错误? - Why is there a wrong number of arguments error when using redirect_to in Rails 4.2? Rails - Geocoder - 。参数数量错误(给定3,预期0..1) - Rails - Geocoder - .near wrong number of arguments (given 3, expected 0..1) Devise中的ArgumentError :: RegistrationsController#新错误的参数个数(2个为0..1) - ArgumentError in Devise::RegistrationsController#new wrong number of arguments (2 for 0..1) 延迟作业和ActionMailer不工作:“错误的参数数量(2为0..1)” - Delayed Job and ActionMailer not working: “wrong number of arguments (2 for 0..1)” 升级到 Rails 6 后参数数量错误(给定 4,预期为 0..1) - wrong number of arguments (given 4, expected 0..1) after upgrading to Rails 6
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM