0

It's been a couple of years since I've done much with Rails and I've no experience with Rails 5.1.6. While I'm able to save data via a seed file and via the Rails Console, I'm unable to save from an HTML form. Instead of saving, on submit I see the authentication token and the params appear in the URL bar of my browser and a re-rendering of the form.

The only thing that is at all unusual is that I develop in Docker containers but the last app that I wrote in 5.1.4 uses the same Dockerfile and Docker-Compose files and it functions normally.

My controller:

class UsersController < ApplicationController
  before_action :find_user, only: [:show, :edit, :update]

  def index
  end

  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    if @user.save
      redirect_to @user
    else
      render :new
    end
  end

  def show
  end

  def edit
  end

  def update
    if @user.update_attributes(user_params)
      redirect_to users_url
    else
      render :edit
    end
  end

  private
    def user_params
      params.require(:user).permit(:last_name, :first_name, :category, :phone_number, :email, :password, :password_confirmation)
    end

    def find_user
      @user = User.find(params[:id])
    end
end

The view:

<%= form_with model: (@user) do |f| %>
      <%= f.label :first_name %>
      <%= f.text_field :first_name %>

      <%= f.label :last_name %>
      <%= f.text_field :last_name %>

      <%= f.label :email %>
      <%= f.text_field :email %>

      <%= f.label :phone_number %>
      <%= f.text_field :phone_number %>

      <%= f.submit "Create user" %>
<% end %>

The model:

class User < ApplicationRecord
  belongs_to :account
end

Development log contents:

Started GET "/users/new?utf8=%E2%9C%93&authenticity_token=r0JIvUKGw1mtIVHT582uIfVvTglMTn6PE2iYOCYNHDv5GJ5AedXb4ZS%2F7kFRmfmFA5PIJeklSbmSE3FR0SpJSg%3D%3D&user%5Bfirst_name%5D=aaa&user%5Blast_name%5D=bbb&user%5Bemail%5D=aaa%40bbb.com&user%5Bphone_number%5D=&commit=Create+user" for 172.18.0.1 at 2018-06-05 21:02:12 +0000
Processing by UsersController#new as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"r0JIvUKGw1mtIVHT582uIfVvTglMTn6PE2iYOCYNHDv5GJ5AedXb4ZS/7kFRmfmFA5PIJeklSbmSE3FR0SpJSg==", "user"=>{"first_name"=>"aaa", "last_name"=>"bbb", "email"=>"aaa@bbb.com", "phone_number"=>""}, "commit"=>"Create user"}
  Rendering users/new.html.erb within layouts/application
  Rendered users/new.html.erb within layouts/application (2.0ms)
Completed 200 OK in 348ms (Views: 331.7ms | ActiveRecord: 0.0ms)

URL after "submit":

http://localhost:3000/users/new?utf8=%E2%9C%93&authenticity_token=r0JIvUKGw1mtIVHT582uIfVvTglMTn6PE2iYOCYNHDv5GJ5AedXb4ZS%2F7kFRmfmFA5PIJeklSbmSE3FR0SpJSg%3D%3D&user%5Bfirst_name%5D=aaa&user%5Blast_name%5D=bbb&user%5Bemail%5D=aaa%40bbb.com&user%5Bphone_number%5D=&commit=Create+user

I've seen this happen before but it's probably been three years back and I cannot recall how I fixed it. Any thoughts would be appreciated.

jvillian
  • 19,953
  • 5
  • 31
  • 44
seanot
  • 13
  • 1
  • 5
  • 1
    Is there a `POST` just before the `GET` that you show in your question? – jvillian Jun 05 '18 at 21:15
  • No, there is not. However, logic tells me there should be. Is my use of "form_with" incorrect in some way? – seanot Jun 05 '18 at 21:21
  • Out of sheer curiosity, what happens if you remove the parentheses around `@user` (without adding `local: true`)? I have a feeling Salil is on to it. – jvillian Jun 05 '18 at 21:25
  • I've tried both ways with no success. I took a peek at a Rails 5.1.4 app in a similar container and in that app, "for_with model: @model_name, local: true" POSTS. In this case, it submits a GET request. – seanot Jun 05 '18 at 21:41
  • Can you coerce with `method: :post`? – jvillian Jun 05 '18 at 21:55
  • 1
    I solved it. My view was a partial that resided inside an HTML form tag (instead of a div). I changed the form tag to a div tag and the GET became a POST. I'm going to mark Salil's solution as the correct solution because I still needed to use "local: true". Thanks also to jvilian for making me think deeper on this. – seanot Jun 05 '18 at 23:51
  • Well, that's a niddly little bug, isn't it? Well done. – jvillian Jun 05 '18 at 23:53

1 Answers1

3

Check this Change

<%= form_with model: (@user) do |f| %>

To

<%= form_with model: @user, local: true do |f| %>
Salil
  • 46,566
  • 21
  • 122
  • 156