链接重定向到错误的视图路径

时间:2016-02-15 02:45:30

标签: ruby-on-rails ruby devise

除设计外,所有链接都运行良好。如果我尝试单击注册或登录,则会将我带到注册/登录页面,对吧?

从网址看起来很正常。 localhost:3000/users/sign_up但是观点是错误的。它显示的是个人资料视图,而不是设计注册/登录视图。

enter image description here

路线代码:

Rails.application.routes.draw do

  resources :questions

  get '/users/:id' => 'profile#profile'
  devise_for :users
  root 'home#index'
end

配置文件控制器:

class ProfileController < ApplicationController
  def profile
  end
end

用户模型:

class User < ActiveRecord::Base
  has_many :questions
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end

问题模型

class Question < ActiveRecord::Base
    belongs_to :user
end

一开始它没有这样做,我认为当我在问题文件夹中编辑show.html.erb视图时就会发生这个错误。

显示代码(问题):

<% @questions.each do |q| %>
    <% if user_signed_in? %>
        <% if q.id == current_user.id %>
            <%= link_to 'Edit', edit_question_path(@question) %>
        <% end %>
    <% end %>
<% end %>

问题控制员:

class QuestionsController < ApplicationController
  before_action :set_question, only: [:show, :edit, :update, :destroy]

  respond_to :html

  def index
    @questions = Question.all
    respond_with(@questions)
  end

  def show
    @questions = Question.all
    respond_with(@question)
  end

  def new
    @question = Question.new
    respond_with(@question)
  end

  def edit
  end

  def create
    @question = Question.new(question_params)
    @question.save
    respond_with(@question)
  end

  def update
    @question.update(question_params)
    respond_with(@question)
  end

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

  private
    def set_question
      @question = Question.find(params[:id])
    end

    def question_params
      params.require(:question).permit(:title, :description, :image_url)
    end
end

1 个答案:

答案 0 :(得分:0)

尝试将get '/users/:id' => 'profile#profile'放在devise_for :users行下面。