Leandro Leandro - 8 months ago 15
Ruby Question

Ruby - Saving a Model shows a Get url in browser

  • Ruby 2.2.4

  • Rails 4.2.6

  • PostgreSQL 9.5

Iam trying to save a simple model, but when i submit the form, my browser url shows this "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"

iam curious about this because other project, it has the same methods, same routes, the only diference is the model that only have one collumn, but it works fine.

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

My controller: notes_controller.rb

def new
@note = Note.new

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

def note_params

my form

<%= 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 class="actions">
<%= f.submit "Create" %>
<% end %>

my routes

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

I saw this post Rails form issuing GET request instead of POST request

but does not work for me.


I fix it thanks to Anthony E, his answer made me look back to code and realize that i have a form inside a form. The outter form was in application.html.erb.

Thanks to all.


Rails can't infer the appropriate form route from your model. Try explicitly setting the form URL and submit method in your form_for:

form_for @note, url: "/notes", as: :note, html: { method: :post } do |f|

Alternatively, it may be simpler to use resourceful routing:

In routes.rb:

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

This will create the following routes:

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