Ruby - 保存模型在浏览器中显示 Get url

Ruby - Saving a Model shows a Get url in browser

我正在尝试保存一个简单的模型,但是当我提交表单时,我的浏览器 url 显示这个“http://localhost:8080/notes/new?utf8=%E2%9C%93&authenticity_token=z0cyVNfUKYWDSDASDWFFZ96zj29UTtDYe8dLlKrI6Mbznb2SrTWNm%2BQ91D2s2AASD2345Fl3fTOneCC2dNg%3D%3D&note%5Btitulo%5D=ddddddd&note%5Bconteudo%5D=dddddddddddddddddd&commit=Create

我很好奇这个,因为其他项目,它有相同的方法,相同的路线,唯一的区别是只有一列的模型,但它工作正常。

def change
    create_table :notes do |t|
      t.text :titulo
      t.text :conteudo
      t.timestamps null: false
    end

我的控制器:notes_controller.rb

  def new
    @note = Note.new
  end

  def create
    @note = Note.new(note_params)
    if @note.save
      redirect_to '/'
    else
      render 'new'
    end
  end

  private
    def note_params
      params.require(:note).permit(:titulo,:conteudo)
    end

我的表格

<%= form_for(@note) do |f| %>
        <div class="field">
          <%= f.label :titulo %><br>
          <%= f.text_area :titulo %>
          <%= f.label :conteudo %><br>
          <%= f.text_area :conteudo %>
        </div>
        <div class="actions">
          <%= f.submit "Create" %>
        </div>
      <% end %>

我的路线

Rails.application.routes.draw do
  root 'notes#index'
  get 'notes/new' => 'notes#new'
  post 'notes' => 'notes#create'

我看到了这个postRails form issuing GET request instead of POST request

但对我不起作用。

编辑:

多亏了 Anthony E,我修复了它,他的回答让我回顾了代码,意识到我有一个表单中的表单。外部形式在 application.html.erb.

感谢大家。

Rails 无法从您的模型中推断出合适的表单路径。尝试在 form_for:

中明确设置表单 URL 和提交方法
form_for @note, url: "/notes", as: :note, html: { method: :post } do |f|
end

或者,使用足智多谋的路由可能更简单:

在routes.rb中:

resources :notes, only: [:new, :create, :index]

这将创建以下路由:

GET /notes/new  # Maps to NotesController#new
POST /notes     # Maps to NotesController#create
GET /notes      # Maps to NotesController#index