Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails rails&Desive,登录后使用在路径中签名后路由到homeshow_Ruby On Rails_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails rails&Desive,登录后使用在路径中签名后路由到homeshow

Ruby on rails rails&Desive,登录后使用在路径中签名后路由到homeshow,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,登录后,我想路由到我的homeindex,但它会生成一个/home/:id请求,并因此路由到homeshow 有人能帮忙看看吗?点击“登录”按钮后,我是否可以路由到索引 非常感谢你 耙道 Prefix Verb URI Pattern Controller#Action new_user_session GET /users/sign_in(.:format) devise/sessions#

登录后,我想路由到我的homeindex,但它会生成一个/home/:id请求,并因此路由到homeshow

有人能帮忙看看吗?点击“登录”按钮后,我是否可以路由到索引

非常感谢你

耙道

                  Prefix Verb   URI Pattern                    Controller#Action
    new_user_session GET    /users/sign_in(.:format)       devise/sessions#new
        user_session POST   /users/sign_in(.:format)       devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy
       user_password POST   /users/password(.:format)      devise/passwords#create
   new_user_password GET    /users/password/new(.:format)  devise/passwords#new
  edit_user_password GET    /users/password/edit(.:format) devise/passwords#edit
                     PATCH  /users/password(.:format)      devise/passwords#update
                     PUT    /users/password(.:format)      devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)        devise/registrations#cancel
   user_registration POST   /users(.:format)               devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)       devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)          devise/registrations#edit
                     PATCH  /users(.:format)               devise/registrations#update
                     PUT    /users(.:format)               devise/registrations#update
                     DELETE /users(.:format)               devise/registrations#destroy
               posts GET    /posts(.:format)               posts#index
                     POST   /posts(.:format)               posts#create
            new_post GET    /posts/new(.:format)           posts#new
           edit_post GET    /posts/:id/edit(.:format)      posts#edit
                post GET    /posts/:id(.:format)           posts#show
                     PATCH  /posts/:id(.:format)           posts#update
                     PUT    /posts/:id(.:format)           posts#update
                     DELETE /posts/:id(.:format)           posts#destroy
               homes GET    /homes(.:format)               homes#index
                     POST   /homes(.:format)               homes#create
            new_home GET    /homes/new(.:format)           homes#new
           edit_home GET    /homes/:id/edit(.:format)      homes#edit
                home GET    /homes/:id(.:format)           homes#show
                     PATCH  /homes/:id(.:format)           homes#update
                     PUT    /homes/:id(.:format)           homes#update
                     DELETE /homes/:id(.:format)           homes#destroy
                root GET    /                              devise/sessions#new
 ->   homes GET    /homes(.:format)               homes#index
 ->   home GET    /homes/:id(.:format)            homes#show
ApplicationController.rb

class ApplicationController < ActionController::Base
def configure_permitted_parameters
  devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation) }
end

protect_from_forgery with: :exception
def after_sign_in_path_for(resource)
  home_path(resource)
end

def after_sign_out_path_for(resource)
  new_user_session_path()
end 
end
class HomesController < ApplicationController
  layout "loginpage"
  def index
  end

  def show
    @heading = "My Home"
  end

  def new
    @home = Home.new
    respond_with(@home)
  end

  def edit
  end

  def create
    @home = Home.new(home_params)
    @home.save
    respond_with(@home)
  end

  def update
    @home.update(home_params)
    respond_with(@home)
  end

  def destroy
    @home.destroy
    respond_with(@home)
  end

  private
    def set_home
      @home = Home.find(params[:id])
    end

    def home_params
      params[:home]
    end
end
class ApplicationController < ActionController::Base

  def after_sign_in_path_for(resource)
    home_path
  end

end
class HomeController < ActionController::Base
  def index
  end
end
家庭控制器.rb

class ApplicationController < ActionController::Base
def configure_permitted_parameters
  devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation) }
end

protect_from_forgery with: :exception
def after_sign_in_path_for(resource)
  home_path(resource)
end

def after_sign_out_path_for(resource)
  new_user_session_path()
end 
end
class HomesController < ApplicationController
  layout "loginpage"
  def index
  end

  def show
    @heading = "My Home"
  end

  def new
    @home = Home.new
    respond_with(@home)
  end

  def edit
  end

  def create
    @home = Home.new(home_params)
    @home.save
    respond_with(@home)
  end

  def update
    @home.update(home_params)
    respond_with(@home)
  end

  def destroy
    @home.destroy
    respond_with(@home)
  end

  private
    def set_home
      @home = Home.find(params[:id])
    end

    def home_params
      params[:home]
    end
end
class ApplicationController < ActionController::Base

  def after_sign_in_path_for(resource)
    home_path
  end

end
class HomeController < ActionController::Base
  def index
  end
end

app/controllers/application_controller.rb

class ApplicationController < ActionController::Base
def configure_permitted_parameters
  devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation) }
end

protect_from_forgery with: :exception
def after_sign_in_path_for(resource)
  home_path(resource)
end

def after_sign_out_path_for(resource)
  new_user_session_path()
end 
end
class HomesController < ApplicationController
  layout "loginpage"
  def index
  end

  def show
    @heading = "My Home"
  end

  def new
    @home = Home.new
    respond_with(@home)
  end

  def edit
  end

  def create
    @home = Home.new(home_params)
    @home.save
    respond_with(@home)
  end

  def update
    @home.update(home_params)
    respond_with(@home)
  end

  def destroy
    @home.destroy
    respond_with(@home)
  end

  private
    def set_home
      @home = Home.find(params[:id])
    end

    def home_params
      params[:home]
    end
end
class ApplicationController < ActionController::Base

  def after_sign_in_path_for(resource)
    home_path
  end

end
class HomeController < ActionController::Base
  def index
  end
end
app/controllers/home_controller.rb

class ApplicationController < ActionController::Base
def configure_permitted_parameters
  devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation) }
end

protect_from_forgery with: :exception
def after_sign_in_path_for(resource)
  home_path(resource)
end

def after_sign_out_path_for(resource)
  new_user_session_path()
end 
end
class HomesController < ApplicationController
  layout "loginpage"
  def index
  end

  def show
    @heading = "My Home"
  end

  def new
    @home = Home.new
    respond_with(@home)
  end

  def edit
  end

  def create
    @home = Home.new(home_params)
    @home.save
    respond_with(@home)
  end

  def update
    @home.update(home_params)
    respond_with(@home)
  end

  def destroy
    @home.destroy
    respond_with(@home)
  end

  private
    def set_home
      @home = Home.find(params[:id])
    end

    def home_params
      params[:home]
    end
end
class ApplicationController < ActionController::Base

  def after_sign_in_path_for(resource)
    home_path
  end

end
class HomeController < ActionController::Base
  def index
  end
end
在Rails中指定静态路由时,只需说出要使用的HTTP操作,然后是要将请求转发到的controlleraction,然后是要设置的_路径名


在您的情况下,您指定要将登录用户发送到home_路径,因此我们通过as:param设置home_路径

您希望在登录后重定向到HomeIndex,但在\u path\u forresource方法中的\u sign\u之后,您的路径将具有home\u pathresource,它将重定向到HomeShow

看看这个,看看你的rake路由输出

                  Prefix Verb   URI Pattern                    Controller#Action
    new_user_session GET    /users/sign_in(.:format)       devise/sessions#new
        user_session POST   /users/sign_in(.:format)       devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy
       user_password POST   /users/password(.:format)      devise/passwords#create
   new_user_password GET    /users/password/new(.:format)  devise/passwords#new
  edit_user_password GET    /users/password/edit(.:format) devise/passwords#edit
                     PATCH  /users/password(.:format)      devise/passwords#update
                     PUT    /users/password(.:format)      devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)        devise/registrations#cancel
   user_registration POST   /users(.:format)               devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)       devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)          devise/registrations#edit
                     PATCH  /users(.:format)               devise/registrations#update
                     PUT    /users(.:format)               devise/registrations#update
                     DELETE /users(.:format)               devise/registrations#destroy
               posts GET    /posts(.:format)               posts#index
                     POST   /posts(.:format)               posts#create
            new_post GET    /posts/new(.:format)           posts#new
           edit_post GET    /posts/:id/edit(.:format)      posts#edit
                post GET    /posts/:id(.:format)           posts#show
                     PATCH  /posts/:id(.:format)           posts#update
                     PUT    /posts/:id(.:format)           posts#update
                     DELETE /posts/:id(.:format)           posts#destroy
               homes GET    /homes(.:format)               homes#index
                     POST   /homes(.:format)               homes#create
            new_home GET    /homes/new(.:format)           homes#new
           edit_home GET    /homes/:id/edit(.:format)      homes#edit
                home GET    /homes/:id(.:format)           homes#show
                     PATCH  /homes/:id(.:format)           homes#update
                     PUT    /homes/:id(.:format)           homes#update
                     DELETE /homes/:id(.:format)           homes#destroy
                root GET    /                              devise/sessions#new
 ->   homes GET    /homes(.:format)               homes#index
 ->   home GET    /homes/:id(.:format)            homes#show
homes\u path被命名为路径/homes的助手,controlleraction为homesindex

home\u pathid被命名为path/home/:id,其中controlleraction为homeshow

试试这个:

如果您有多个设备型号

class ApplicationController < ActionController::Base

 private

 def after_sign_in_path_for(resource)
    if resource.is_a?(YourDeviceModel)
        homes_path
    else
        another_path 
    end
 end
end
如果您有单设备型号

class ApplicationController < ActionController::Base

  private

  def after_sign_in_path_for(resource)
        homes_path
  end

end
谢谢你的评论

Q:如果没有s,它将路由到显示方法


可能并非总是如此,您拥有资源:routes上的homes.rb它可以生成rails默认的路由。您可以阅读此

在applicationController中尝试使用homes\u路径吗@非常感谢你!!!!!!!!!它起作用了。。为什么…我整个上午都在这上面…所以,如果没有s,它将是显示方法的路线?