2016-02-11 4 views
2

Это мой первый вопрос здесь, поэтому я надеюсь, что я не спросил его неправильно.Новый билет создается при загрузке новой формы

У меня есть новое новое действие на моем контроллере билетов. Когда я загружаю билеты/новый, он создает новый элемент в БД и совершает его.

Вот результат работы сервера при загрузке страницы.

Started GET "/tickets/new" for ::1 at 2016-02-10 21:14:47 -0800 
Processing by TicketsController#new as HTML 
    Customer Load (0.4ms) SELECT `customers`.* FROM `customers` WHERE `customers`.`email` = '[email protected]' LIMIT 1 
    (0.3ms) BEGIN 
    SQL (0.5ms) INSERT INTO `tickets` (`category`, `created_at`, `updated_at`) VALUES (3, '2016-02-11 05:14:47', '2016-02-11 05:14:47') 
    (6.4ms) COMMIT 
    Rendered tickets/_new_form.html.erb (23.3ms) 
    Rendered tickets/new.html.erb within layouts/application (48.4ms) 
    Rendered layouts/_user_nav.html.erb (0.8ms) 
    Rendered layouts/_navbar.html.erb (0.5ms) 
    Rendered layouts/_flashes.html.erb (0.5ms) 
    Rendered layouts/_minimal.html.erb (759.5ms) 
Completed 200 OK in 893ms (Views: 822.1ms | ActiveRecord: 21.3ms) 

Это из контролера билетов.

def new 
    @ticket = Ticket.new 
end 

Вот код для формы.

<%= form_for(@ticket, html: { class: 'form-horizontal' }) do |f| %> 
    <%= f.error_notification %> 
    <%= f.hidden_field(:category) %> 
    <%= f.hidden_field(:severity) %> 


    <br> 
    <%= f.form_group :summary do |f| %> 
     <%= f.label :summary, class: 'control-label col-md-2' %> 
     <div class='col-md-8'> 
     <%= f.text_field :summary, class: 'form-control' %> 
     <%= f.error_messages %> 
     </div> 
    <% end %> 

    <%= f.form_group :detail do |f| %> 
     <%= f.label :detail, class: 'control-label col-md-2' %> 
     <div class='col-md-8'> 
     <%= f.text_area :detail, class: 'form-control' %> 
     <%= f.error_messages %> 
     </div> 
    <% end %> 
    <br> 


    </div> 

    <div class="form-actions col-md-offset-2 col-md-10"> 
    <%= f.submit 'Create', class: 'btn btn-primary' %> 
    <%= link_to "Cancel", tickets_path, class: 'btn' %> 
    </div> 
<% end %> 

Вот соответствующие маршруты.

resources :tickets do 
    collection do 
     get :step_1 
     get :new_ticket 
     get :billing_new_1 
     get :internet_step_1 
     get :internet_step_2 
     get :internet_modem_reset 
     get :internet_step_1 
     get :internet_step_2 
     get :internet_create_1 
     get :internet_create_2 
     get :tv_step_1 
     get :tv_step_2 
     get :tv_step_3 
     get :tv_create_1 
     get :tv_create_2 
     get :tv_create_3 
     get :closed 
     get :sidenav 
    end 
    member do 
     put :close 
    end 

    resources :notes 
    resources :appointments 
    end 

Справка!

--Tim

+1

выслать свои маршруты пожалуйста – Cyzanfar

+0

ваш файл маршрута ... скопируйте его в свой вопрос. НЕ КАК КОММЕНТАРИЙ. Пожалуйста. – Cyzanfar

+0

Что произойдет, если вы удалите 'get: new_ticket'? – Cyzanfar

ответ

0
INSERT INTO `tickets` (`category`, `created_at`, `updated_at`) VALUES (3, '2016-02-11 05:14:47', '2016-02-11 05:14:47') 

Это становится category (3) откуда-то, предполагая, что есть некоторые функциональные возможности где который спасает @ticket.

Простейшее объяснение, которое я вижу, это то, что у вас есть before_action. Это было бы полезно, чтобы показать свои целые TicketsController:

#app/controllers/tickets_controller.rb 
class TicketsController < ApplicationController 
    before_action :set_user #-> something like this?? 
end 

Поскольку вы новичок, вы можете сделать ваши маршруты гораздо более емким (multiple resources):

#config/routes.rb 
methods = %i(step_1 new_ticket billing_new_1 internet_step_1 internet_step_2 internet_modem_reset internet_create_1 internet_create_2 tv_step_1 tv_step_2 tv_step_3 tv_create_1 tv_create_2 tv_create_3 closed sidenav) 

resources :tickets do 
    resources :notes, :appointments 
    collection do 
    methods.each {|method| get method } 
    end 
    put :close, on: :member 
end 
+0

Благодарим вас за вход по маршрутам hte. Я это сделаю. –

0

Я в конечном итоге, начиная весь билет класс выше. Я думаю, что ошибка была в моем html. Я предполагаю, что оператор if с ошибкой вызывал сохранение билета из-за перечисления с этим именем в модели.

Вот что я считаю плохим html.

<% if @ticket.category == :tv %> 
      Ok. Your tv is down, but your internet is still working. 
      <br> 
      Do you have any more details to add? If so, add them here. If not, just hit sumbit and we will open up a ticket with all of the information that you have provided us. 

      <% elsif @ticket.internet! %> 
      Ok. Your internet is down, but your tv is still working. 
      <br> 
      Do you have any more details to add? If so, add them here. If not, just hit sumbit and we will open up a ticket with all of the information that you have provided us. 

      <% elsif @ticket.billing %> 
      I am fresh out of questions. 
      <br> 
      Do you have any more details to add? If so, add them here. If not, just hit sumbit and we will open up a ticket with all of the information that you have provided us. 
      <% elsif @ticket.category == :internet_and_tv %> 
      Ok. Your cable and internet are both down.'%> 
      <br> 
      Do you have any more details to add? If so, add them here. If not, just hit sumbit and we will open up a ticket with all of the information that you have provided us.'%> 
      <% else @ticket.category == :plant %> 
      <%end%> 
Смежные вопросы