Ruby - Saving a Model shows a Get url in browser -
- ruby 2.2.4
- rails 4.2.6
- postgresql 9.5
i trying save simple model, when submit form, browser url shows "http://localhost:8080/notes/new?utf8=%e2%9c%93&authenticity_token=z0cyvnfukywdsdasdwffz96zj29uttdye8dllkri6mbznb2srtwnm%2bq91d2s2aasd2345fl3ftonecc2dng%3d%3d¬e%5btitulo%5d=ddddddd¬e%5bconteudo%5d=dddddddddddddddddd&commit=create"
i curious because other project, has same methods, same routes, difference model have 1 column, works fine.
def change create_table :notes |t| t.text :titulo t.text :conteudo t.timestamps null: false end
my controller: 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
my form
<%= form_for(@note) |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 %>
my routes
rails.application.routes.draw root 'notes#index' 'notes/new' => 'notes#new' post 'notes' => 'notes#create'
i saw post rails form issuing request instead of post request
but not work me.
edit:
i fix anthony e, answer made me code , realize have form inside form. outer form in application.html.erb.
thanks all.
rails can't infer appropriate form route model. try explicitly setting form url , submit method in form_for
:
form_for @note, url: "/notes", as: :note, html: { method: :post } |f| end
alternatively, may simpler use resourceful routing:
in routes.rb:
resources :notes, only: [:new, :create, :index]
this create following routes:
get /notes/new # maps notescontroller#new post /notes # maps notescontroller#create /notes # maps notescontroller#index
Comments
Post a Comment