创建操作的自定义 POST 路由不起作用
Custom POST routes for create action not working
解释上下文
“我正在学习 Rails,构建社交应用 f
“我认为我的路线问题......
“……还在想办法。
“ 3 天尝试各种事情。
“这就是我现在所处的位置,有些事情没有按预期进行。
“任何 help/pointers 将不胜感激!:)
问题
使用 devise 登录后,我路由到名为“/opinions”的主页
当我访问表单以创建一个新论坛并点击提交时,“
表格 post 到 '/opinions' 正确"
但我在我的用户页面“/users/1”中呈现相同的表单
当我点击提交时,我 git 这个错误。"
## The log
Started POST "/users/2" for ::1 at 2020-10-22 00:05:18 +0300
ActionController::RoutingError (No route matches [POST] "/users/2"):
## config/routes.rb
resources :followers
devise_for :users
devise_scope :user do
root to: 'devise/sessions#new'
end
resources :users, only: [:index, :show, :create]do
resources :followers, only: [:create, :destroy]
end
resources :opinions
## _form.html.erb
<%= form_with(model: @opinions, local: false) do |form| %>
<div class="field rich p-3">
<div class="control">
<%= form.rich_text_area :body, label: "What's Your Opinion", input_html: {class: "textarea"}, wrapper: false, label_html: {class: "label"}, placeholders: "Your opinion...", autofocuse: true %>
</div>
</div>
<%= form.button :submit, class: "button is-info" %>
<% end %>
## Rake Routes
root GET / devise/sessions#new
user_followers POST /users/:user_id/followers(.:format) followers#create
user_follower DELETE /users/:user_id/followers/:id(.:format) followers#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
user GET /users/:id(.:format) users#show
opinions GET /opinions(.:format) opinions#index
POST /opinions(.:format) opinions#create
new_opinion GET /opinions/new(.:format) opinions#new
edit_opinion GET /opinions/:id/edit(.:format) opinions#edit
opinion GET /opinions/:id(.:format) opinions#show
PATCH /opinions/:id(.:format) opinions#update
## opinions_controller
def create
@opinion = @user.opinions.build(opinion_params)
respond_to do |format|
if @opinion.save
format.html { redirect_to opinions_url, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: @opinion }
else
format.html { render :new }
format.json { render json: @opinion.errors, status: :unprocessable_entity }
end
end
end
## users_controller
class UsersController < ApplicationController
include TheUser
include ActionText::Attachable
before_action :set_user
before_action :authenticate_user!
before_action :user_signed_in?
def index
@users = User.all
@mutual_friends = User.where(id: show_two_friends)
end
def show
@user = User.find(params[:id])
@opinion = @user.opinions
respond_to do |format|
if @user
format.html
format.js { @current_user = @user }
format.json { render :show, status: :created, location: @user }
else
format.html { render :new }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
end
我做错了什么请帮助我?
好吧,错误已分派,因为您不应该对 /resource/:id (/users/2) 进行 POST。如果您希望更新信息,请使用 PUT。我认为您正在将 @opinions 传递给 form_with 助手,我猜这是用户的一系列意见。如果是这样,您应该创建一个变量@opinion = Opinion.new 以在用户视图中正确执行。但请使用 UserController.rb 文件编辑 post 以更好地帮助您。
正确的做法应该是这样的:
##UserController.rb
...
def show ## I will guess that you are using Opinion form in User Show
@user = User.find(:id)
@opinions = @user.opinions
@new_opinion = Opinion.new
end
...
## app/views/users/show.html.erb
...
<%= form_with(model: @new_opinion, local: false) do |form| %>
<%= render 'opinions/form' form: form %>
<% end %>
...
## app/views/opinions/_form.html.erb
...
<div class="field rich p-3">
<div class="control">
<%= form.rich_text_area(
:body,
label: "What's Your Opinion",
input_html: {class: "textarea"},
wrapper: false,
label_html: {class: "label"},
placeholders: "Your opinion...",
autofocuse: true
) %>
</div>
...
</div>
...
<%= form.button :submit, class: "button is-info" %>
...
请记住,部分表单不应包含 form_with 标记。使用render标签的局部变量传递form属性
希望能帮到你。祝你好运!
解释上下文
“我正在学习 Rails,构建社交应用 f “我认为我的路线问题...... “……还在想办法。 “ 3 天尝试各种事情。 “这就是我现在所处的位置,有些事情没有按预期进行。 “任何 help/pointers 将不胜感激!:)
问题
使用 devise 登录后,我路由到名为“/opinions”的主页 当我访问表单以创建一个新论坛并点击提交时,“ 表格 post 到 '/opinions' 正确" 但我在我的用户页面“/users/1”中呈现相同的表单 当我点击提交时,我 git 这个错误。"
## The log
Started POST "/users/2" for ::1 at 2020-10-22 00:05:18 +0300
ActionController::RoutingError (No route matches [POST] "/users/2"):
## config/routes.rb
resources :followers
devise_for :users
devise_scope :user do
root to: 'devise/sessions#new'
end
resources :users, only: [:index, :show, :create]do
resources :followers, only: [:create, :destroy]
end
resources :opinions
## _form.html.erb
<%= form_with(model: @opinions, local: false) do |form| %>
<div class="field rich p-3">
<div class="control">
<%= form.rich_text_area :body, label: "What's Your Opinion", input_html: {class: "textarea"}, wrapper: false, label_html: {class: "label"}, placeholders: "Your opinion...", autofocuse: true %>
</div>
</div>
<%= form.button :submit, class: "button is-info" %>
<% end %>
## Rake Routes
root GET / devise/sessions#new
user_followers POST /users/:user_id/followers(.:format) followers#create
user_follower DELETE /users/:user_id/followers/:id(.:format) followers#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
user GET /users/:id(.:format) users#show
opinions GET /opinions(.:format) opinions#index
POST /opinions(.:format) opinions#create
new_opinion GET /opinions/new(.:format) opinions#new
edit_opinion GET /opinions/:id/edit(.:format) opinions#edit
opinion GET /opinions/:id(.:format) opinions#show
PATCH /opinions/:id(.:format) opinions#update
## opinions_controller
def create
@opinion = @user.opinions.build(opinion_params)
respond_to do |format|
if @opinion.save
format.html { redirect_to opinions_url, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: @opinion }
else
format.html { render :new }
format.json { render json: @opinion.errors, status: :unprocessable_entity }
end
end
end
## users_controller
class UsersController < ApplicationController
include TheUser
include ActionText::Attachable
before_action :set_user
before_action :authenticate_user!
before_action :user_signed_in?
def index
@users = User.all
@mutual_friends = User.where(id: show_two_friends)
end
def show
@user = User.find(params[:id])
@opinion = @user.opinions
respond_to do |format|
if @user
format.html
format.js { @current_user = @user }
format.json { render :show, status: :created, location: @user }
else
format.html { render :new }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
end
我做错了什么请帮助我?
好吧,错误已分派,因为您不应该对 /resource/:id (/users/2) 进行 POST。如果您希望更新信息,请使用 PUT。我认为您正在将 @opinions 传递给 form_with 助手,我猜这是用户的一系列意见。如果是这样,您应该创建一个变量@opinion = Opinion.new 以在用户视图中正确执行。但请使用 UserController.rb 文件编辑 post 以更好地帮助您。
正确的做法应该是这样的:
##UserController.rb
...
def show ## I will guess that you are using Opinion form in User Show
@user = User.find(:id)
@opinions = @user.opinions
@new_opinion = Opinion.new
end
...
## app/views/users/show.html.erb
...
<%= form_with(model: @new_opinion, local: false) do |form| %>
<%= render 'opinions/form' form: form %>
<% end %>
...
## app/views/opinions/_form.html.erb
...
<div class="field rich p-3">
<div class="control">
<%= form.rich_text_area(
:body,
label: "What's Your Opinion",
input_html: {class: "textarea"},
wrapper: false,
label_html: {class: "label"},
placeholders: "Your opinion...",
autofocuse: true
) %>
</div>
...
</div>
...
<%= form.button :submit, class: "button is-info" %>
...
请记住,部分表单不应包含 form_with 标记。使用render标签的局部变量传递form属性
希望能帮到你。祝你好运!