簡體   English   中英

用於創建操作的自定義 POST 路由不起作用

[英]Custom POST routes for create action not working

解釋上下文

“我正在學習 Rails,構建 asocial 應用程序 f “我認為我的路線問題......“......仍在弄清楚它們。” 3 天嘗試各種事情。 “這就是我現在所處的位置,有些事情沒有按預期工作。”任何幫助/指針將不勝感激! :)

問題

使用設備登錄后,我路由到名為“/意見”的主頁當我訪問表單以創建新論壇並點擊提交時,“表單正確發布到'/意見'”但我正在呈現相同的表單我的用戶頁面 '/users/1" 當我在那里點擊提交時,我出現了這個錯誤。"

## 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 helper,我猜這是一個用戶意見的數組。 如果是這樣,您應該創建一個變量 @opinion = Opinion.new 以在用戶視圖中正確執行。 但是請使用 UserController.rb 文件編輯帖子以更好地幫助您。

正確的方法應該是這樣的:

##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 標記。 使用渲染標簽的局部變量來傳遞表單屬性。

希望我有所幫助。 祝你好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM