[英]wrong number of arguments (2 for 0..1) at redirect_to
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.