我对此非常陌生,我正在尝试实现一个待办事项列表,该列表允许通过用户身份验证使用标记。更新任务时,出现路由错误,指出没有路由与[PATCH]“ / users / 1 / articles”相匹配。我怀疑这是因为更新文章时我没有传递文章ID。有人可以帮助指导我如何解决此问题吗?任何建议将不胜感激,谢谢!
错误页面上的路由
<img src="images/1.gif">
Routes.rb
user_article_path GET /users/:user_id/articles/:id(.:format)
articles#show
PATCH /users/:user_id/articles/:id(.:format)
articles#update
PUT /users/:user_id/articles/:id(.:format)
articles#update
DELETE /users/:user_id/articles/:id(.:format)
articles#destroy
article.rb
Rails.application.routes.draw do
get 'sessions/new'
get 'welcome/index'
get '/signup', to: 'users#new'
post '/signup', to: 'users#create'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
resources :users do
resources :articles
end
get 'tags/:tag', to: 'articles#index', as: :tag, :constraints => { :tag => /[^\/]+/ }
root 'welcome#index'
end
articles_controller
class Article < ApplicationRecord
attr_accessor :content, :name, :tag_list
before_create :downcase_fields
has_many :taggings , dependent: :destroy
has_many :tags, through: :taggings, dependent: :destroy
belongs_to :user
validates :user_id, presence: true
validates :title, presence: true,
length: { minimum: 1}
def self.tagged_with(name)
Tag.find_by_name!(name).articles
end
def downcase_fields
self.title.downcase
end
def self.tag_counts
Tag.select("tags.*, count(taggings.tag_id) as count").
joins(:taggings).group("taggings.tag_id")
end
def tag_list
tags.map(&:name).join(", ")
end
def tag_list=(names)
self.tags = names.split(",").map do |n|
Tag.where(name: n.strip).first_or_create!
end
end
def self.search(term)
if term
where('title LIKE ?', "%#{term}%").order('id DESC')
else
order('id DESC')
end
end
end
db.schema.rb
class ArticlesController < ApplicationController
before_action :correct_user
def index
@user = User.find(params[:user_id])
@articles = @user.articles.search(params[:term])
# if params[:tag]
# @articles = @user.articles.tagged_with(params[:tag])
# else
# @articles = @user.articles.all
# end
end
def show
@user = User.find(params[:user_id])
@article = @user.articles.find(params[:id])
end
def new
@user = User.find(params[:user_id])
@article = @user.articles.new
end
def edit
@user = User.find(params[:user_id])
@article = @user.articles.find(params[:id])
end
def create
@user = User.find(params[:user_id])
@article = @user.articles.new(article_params)
if @article.save
redirect_to user_articles_url
else
render 'new'
end
end
def update
@user = User.find(params[:user_id])
@article = @user.articles.find(params[:id])
if @article.update(article_params)
redirect_to user_articles_path
else
render 'edit'
end
end
def destroy
@user = User.find(params[:user_id])
@article = @user.articles.find(params[:id])
@article.destroy
redirect_to user_articles_path
end
private
def article_params
params.require(:article).permit(:title, :text, :tag_list, :term)
end
# Confirms a logged_in user_
def logged_in_user
unless logged_in?
store_location
flash[:danger] = "Please log in."
redirect_to login_url
end
end
# Confirms the correct user
def correct_user
@user = User.find(params[:user_id])
redirect_to(root_url) unless current_user?(@user)
end
end
用户控制器
ActiveRecord::Schema.define(version: 2019_01_27_093653) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "articles", force: :cascade do |t|
t.string "title"
t.text "text"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "user_id"
t.index ["user_id"], name: "index_articles_on_user_id"
end
create_table "taggings", force: :cascade do |t|
t.bigint "tag_id"
t.bigint "article_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "user_id"
t.index ["article_id"], name: "index_taggings_on_article_id"
t.index ["tag_id"], name: "index_taggings_on_tag_id"
t.index ["user_id"], name: "index_taggings_on_user_id"
end
create_table "tags", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.string "remember_digest"
t.index ["email"], name: "index_users_on_email", unique: true
end
add_foreign_key "articles", "users"
add_foreign_key "taggings", "articles"
add_foreign_key "taggings", "tags"
add_foreign_key "taggings", "users"
end
_form.html.erb(创建和更新共享同一表格)
class UsersController < ApplicationController
before_action :logged_in_user, only: [:index, :edit, :update]
before_action :correct_user, only: [:edit, :update]
def show
@user = User.find(params[:id])
redirect_to user_articles_path(@user)
end
def new
@user = User.new
end
def index
@users = User.paginate(page: params[:page])
end
def create
@user = User.new(user_params)
if @user.save
log_in @user
flash[:success] = "Welcome to the To-Do-Manager!"
redirect_to user_articles_path(@user)
else
render 'new'
end
end
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
if @user.update_attributes(user_params)
flash[:success] = "Profile updated"
redirect_to @user
else
render 'edit'
end
end
private
答案 0 :(得分:1)
根据您的路线,您的用户文章路径应为单数:
<%= form_with model: @article, url: user_article_path(@user), local: true do |form| %>
答案 1 :(得分:0)
您的路由需要两个参数:用户(:user_id)和商品(:id),您仅传递用户user_articles_path(@user)
并使用复数形式,并且该路由在该HTTP中不存在方法。
您必须使用url: user_article_path(@user,@article)
(或者您可以使用快捷方式版本url: [@user, @article]
)。