Кнопка отправки формы Rails нажимает «новый» вместо «создать» - PullRequest
0 голосов
/ 01 апреля 2020

Я не могу понять, что заставляет мой код нажимать / обновлять вместо «создать» при отправке. Я использую bootstrap_form_with и пытался вернуться обратно к form_with, но не повезло.

Вот мой контроллер:

class AccountsController < ApplicationController
  before_action :find_account, only: [:show, :edit, :update, :destroy]

  def index
    @accounts = Account.all

  end

  def show 

  end

  def new 

  end

  def create 

    @account = Account.new(account_params) 
      if @account.save 
        flash[:notice] = "Account created sucsessfully"
        redirect_to @account
      else    
        flash.now[:error] = @item.errors.full_messages.to_sentence
       render :new 
    end
  end

  def edit   
  end

  def update
     if @account.update(account_params)
      flash[:notice] = "Account created sucsessfully"
      redirect_to @account 
    else    
      flash.now[:error] = @item.errors.full_messages.to_sentence
      render :edit 
    end
  end

  def destroy
    @account.delete
    redirect_to accounts_path
  end

private 

def find_account 
  @account = Account.find(params[:id])
end

def account_params
  byebug 
    params.permit(
      :account_name,
      :account_address_line1,
      :account_address_line2,
      :account_city,
      :account_state,
      :account_zipcode,
      :account_ppu,
      :account_notes,
      :contact_first_name,
      :contact_last_name,
      :contact_phone,
      :contact_email,
      :authorization_ids => []
    )
  end

end 


Мои виды представлений выглядят так:

<% states = [ 'AL', 'AK', 'AS', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DC', 'DE', 'DC', 'FL', 'GA', 'HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MH', 'MD', 'MA', 'MI', 'MN', 'MS', 'MO', 'MT', 'NE', 'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'MP', 'OH', 'OK', 'OR', 'PA' 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VT' 'VA', 'WA', 'WV', 'WI', 'WY'] %>
<% ppu = ['Employment', 'Insurance', 'Law Enforement'] %>

<%= bootstrap_form_with(model: @account, local: true) do |f| %>
<%= f.text_field :account_name %>
<%= f.text_field :account_address_line1 %>
<%= f.text_field :account_address_line2 %>
<%= f.text_field :account_city %>
<%= f.select :account_state, states %>
<%= f.text_field :account_zipcode %>
<%= f.text_field :contact_first_name %>
<%= f.text_field :contact_last_name %>
<%= f.email_field :contact_email %>
<%= f.telephone_field :contact_phone %>
<%= f.select :account_ppu, ppu, placeholder: "Select PPU...." %>
<%= f.text_area :account_notes %>
<%= f.collection_check_boxes :authorization_ids, Authorization.all, :id, :auth_name %>
<%= f.submit 'Submit', class: "btn btn-primary" %>
<% end %>

это частичный файл _form.html.erb

<div class="row justify-content-center">
    <div class="col-lg-16">
        <h3>Create Account</h3>
    </div>
</div>

<%= render partial: 'form', account: @account %>

Маршруты:

Rails.application.routes.draw do


  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
  root "accounts#index"
  resources :accounts
  resources :authorizations
end

Может кто-нибудь определить, где я иду не так?

...