1
除了設計,所有鏈接都工作正常。如果我嘗試點擊註冊或登錄,則需要我註冊/登錄頁面,對不對?鏈接重定向到錯誤的視圖路徑
從URL看起來很正常。 localhost:3000/users/sign_up
但看法是錯誤的。它顯示的是配置文件視圖,而不是設計註冊/登錄視圖。
路由代碼:
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
那太奇怪了 – Raymond
在'config/routes.rb'文件中排序在Rails中很重要。我認爲設計已經聲明瞭'users /:id'路徑,所以它覆蓋了你的'profile'路徑。 –