我无法弄清楚是什么原因导致我的代码在提交时命中/ new而不是“ create”。我正在使用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
有人可以发现我要去哪里了吗?