New ticket is being created when loading a new form - ruby-on-rails

This is my first question on here, so I am hoping I have not asked it incorrectly.
I have a generic new action on my tickets controller. Whenever I load tickets/new, it is creating a new item in the DB and committing it.
Here is the output from the server when the page is loading.
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` = 'tim#tim.com' 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)
This is the from the tickets controller.
def new
#ticket = Ticket.new
end
Here is the code for the form.
<%= 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 %>
Here are the relevant routes.
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
Help!!
--Tim

INSERT INTO `tickets` (`category`, `created_at`, `updated_at`) VALUES (3, '2016-02-11 05:14:47', '2016-02-11 05:14:47')
This is getting category (3) from somewhere, suggesting that there is some functionality somewhere which is saving the #ticket.
The simplest explanation I can see is that you have a before_action somewhere. It would benefit to show your entire TicketsController:
#app/controllers/tickets_controller.rb
class TicketsController < ApplicationController
before_action :set_user #-> something like this??
end
Since you're new, you can make your routes much more succinct (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

I ended up starting the whole ticket class over.
I think the error was in my html.
My guess is that the if statement with the bang was causing the ticket to save, because of an enum with that name on the model.
Here is what I think was the bad 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%>

Related

params being passed from form_for but not inserted/commited to database

I have a form submitting a value to my create controller. In the server log, I see the parameter (:style) being passed, but not inserted into database. Does someone know how that could happen?
Server log
Started POST "/users/25/cupboards" for ::1 at 2016-08-19 19:11:39 -0400
Processing by CupboardsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"asWeoSFSisr62oMOPXZnb//KJ8LnAhT1h07NOm1Yn2O8t06N4yrDGugkd01AMQcujYzSahH+O7bEA/jpH+L9fQ==", "style"=>"WEEKEND", "commit"=>"Make a new closet", "user_id"=>"25"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 25]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 25]]
(0.1ms) begin transaction
SQL (0.5ms) INSERT INTO "cupboards" ("user_id", "created_at", "updated_at") VALUES (?, ?, ?) [["user_id", 25], ["created_at", "2016-08-19 23:11:39.219003"], ["updated_at", "2016-08-19 23:11:39.219003"]]
(0.7ms) commit transaction
Redirected to http://localhost:3000/users/25/cupboards/35/edit
Completed 302 Found in 46ms (ActiveRecord: 1.4ms)
Form
<%= form_for #cupboard, url: { action: "create" }, html: { class: "listform"} do |f| %>
<%= f.label "ATHLEISURE", class: "welcomelistitem" %>
<%= radio_button_tag(:style, "ATHLEISURE") %>
<%= f.label "CASUAL", class: "welcomelistitem" %>
<%= radio_button_tag(:style, "CASUAL") %>
<%= f.label "PROFESISONAL", class: "welcomelistitem" %>
<%= radio_button_tag(:style, "PROFESSIONAL") %>
<%= f.label "WEEKEND", class: "welcomelistitem" %>
<%= radio_button_tag(:style, "WEEKEND") %>
<%= f.label "FESTIVAL", class: "welcomelistitem" %>
<%= radio_button_tag(:style, "FESTIVAL") %>
<%= f.label "DATENIGHT", class: "welcomelistitem" %>
<%= radio_button_tag(:style, "DATENIGHT") %>
<%= f.submit 'Make a new closet' %>
<% end %>
Controller
def create
#user = User.find(params[:user_id])
#cupboard = #user.cupboards.new(cupboard_params)
if #cupboard.save
redirect_to edit_user_cupboard_path(#user, #cupboard)
else
render :new
end
end
Params
private
def cupboard_params
# I used .fetch because .permit wasnt working. workaround found at http://stackoverflow.com/questions/24944871/actioncontrollerparametermissing-param-is-missing-or-the-value-is-empty-film
params.fetch(:cupboard, {}).permit(:style, :season, :neutral1, :neutral2, :accent1, :accent2)
end
end
Thank you for the assistance!
This happens because your form submits the styleattribute value like style instead of cupboard[style]. Looks like you're mixing form_for methods with form_tag ones and you need to replace radio_button_tag with radio_button. Also note that there are 2 forms of notations: either the function invocation radio_button(:model_name, :col_name, 'value') or the method invocation on the form object itself f.radio_button(:col_name, 'value'). Another issue with your form is that you're using label for each radio while it's designed to be used once for every group of radio buttons. Hence, the form should look like this:
<%= form_for #cupboard, html: { class: 'listform' } do |f| %>
<%= f.label :style, class: 'welcomelistitem' %>
<%= f.radio_button :style, 'ATHLEISURE' %>
<%= f.radio_button :style, 'CASUAL' %>
<%= f.radio_button :style, 'PROFESSIONAL' %>
<%= f.radio_button :style, 'WEEKEND' %>
<%= f.radio_button :style, 'FESTIVAL' %>
<%= f.radio_button :style, 'DATENIGHT' %>
<%= f.submit 'Make a new closet' %>
<% end %>
You'll then have to rewrite the cupboard_params method like this:
def cupboard_params
params.require(:cupboard).permit(:style)
end
Didn't have time to check if this works, but seems like it should.
The parameter "style" is not being passed inside of params[:cupboard]. It is directly in params.
I don't see the other params (besides style) in your form so I'm not sure what is going on there but try this
def cupboard_params
params.permit(:style)
end
Or change your radio buttons in the view to this
radio_button_tag('cupboard[style]', "ATHLEISURE")

Unable to create entry in join table

I'm attempting to save an entry to a join table, but have been unsuccessful. As I have it configured, Stockholder has_many :entity_joins (I'm attempting to allow a Stockholder to consist of multiple people and to allow a Person to participate with multiple stockholders. Each join entry for a single stockholder is added with cocoon as follows:
<h1>Stockholders#edit</h1>
<p>Find me in app/views/stockholders/edit.html.erb</p>
<%= simple_form_for [#stock, #stockholder], html: {id:"stockholderform"}, update: { success: "response", failure: "error"} do |f| %>
<div class="container">
<div class="symegrid">
<div class="form-inline">
<%= simple_fields_for :entity_joins do |ejoin| %>
<%= render 'entity_join_fields', f:ejoin %>
<% end %>
<div>
<%= link_to_add_association 'Add Owner', f, :entity_joins, class: "btn btn-default add-button" %>
</div>
The entity_join_fields just consist of a single field to indicate which person should be added to the Stockholder via a select menu:
<%= f.grouped_collection_select :entity_id, [Org, Person], :all, :model_name, :to_global_id, lambda {|org_or_person_object| org_or_person_object.instance_of? Org? rescue org_or_person_object.fname + " " + org_or_person_object.lname rescue org_or_person_object.name}, label:"Stockholder", class: "names"%>
But when I go to save the Stockholder form, the parameters in the development.log are as follows:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"JF6a/hVU49y1AAX4iAyi6t/Y/ti1S6CAotw28Qj8PGbESpl/puMUliqOz9iwUQ9vHwIeF5hFjykwSUTcKpzGrQ==", "entity_joins"=>{"entity_id"=>"gid://legal/Person/2"}, "stockholder"=>{"cert_number"=>"", "issue_date(2i)"=>"12", "issue_date(3i)"=>"2", "issue_date(1i)"=>"2015", "shares_issued"=>"", "shares_repurchased"=>"", "shares_canceled"=>"", "shares_outstanding"=>""}, "commit"=>"Update Stockholder", "stock_id"=>"1", "id"=>"2"}
The most striking thing about it is that entity_joins falls outside of stockholder params and is not nested within it. Obviously, it isn't saving to the database and no errors are being thrown either. The data for entity_joins is correct (it's the global id for the Person so that I might be able to expand the join to be polymorphic but that's another story). Is there anything obvious that might be causing this issue?
Thanks in advance.
You need to call simple_fields_for on the form builder instance:
<%= f.simple_fields_for :entity_joins do |ejoin| %>
<%= render 'entity_join_fields', f:ejoin %>
<% end %>
Otherwise it does not have a context and will just create inputs with name="entity_joins[something]" instead of stockholder[entity_joins][something].

Rails 4 nested attribute hash keys showing as unpermitted

I have a Category model and a Standard model. A category has many standards through a "join table" model CategoryStandard. In my view, I have a form where I can edit the category description, and then add or remove standards from that category. So, my nested attributes are for :category_standards, because I'm not editing the standard itself, just adding or removing relationships, if that makes sense.
Here's the important part of the view:
<%= form_for(#category) do |f| %>
...
<div class="field">
<%= f.label :description %>
<%= f.text_field :description %>
</div>
<%= label_tag nil, "Standards in this Category" %>
<div id="standard-list">
<%= f.fields_for :category_standards do |ff| %>
<div class="field">
<%= ff.object.standard.number_with_exceptions %>
<%= ff.hidden_field :standard_id %>
<%= ff.hidden_field :_destroy %>
<%= link_to "<span class='glyphicon glyphicon-remove'></span>".html_safe, "", class: "del-std-btn", title: "Remove standard from category" %>
</div>
<% end %>
<div class="hidden" id="std-add-new-template">
<div class="field">
<%= f.fields_for :category_standards, CategoryStandard.new, child_index: "new_id" do |ff| %>
<%= ff.collection_select :standard_id, #standards - #category.standards, :id, :number_with_exceptions, prompt: "Select a standard to add" %>
<% end %>
</div>
</div>
</div>
...
<% end %>
There's some jQuery under the hood to manipulate the "rows", but that works fine and I don't think it's part of my problem, so I'll omit it.
In my Category model, I have:
class Category < ActiveRecord::Base
has_many :category_standards, dependent: :destroy
has_many :standards, through: :category_standards
validates :description, presence: true,
uniqueness: true
accepts_nested_attributes_for :category_standards, allow_destroy: true, reject_if: proc { |attributes| attributes['standard_id'].blank?}
end
And in my Categories controller, I have:
def category_params
params.require(:category).permit(:description, category_standards_attributes: [:id, :standard_id, :_destroy])
end
But when I try to add a standard to a category, I get these lines in my server log (reformatted in the hopes of making it more readable):
Parameters: {"utf8"=>"✓",
"authenticity_token"=>"***********",
"category"=>{
"description"=>"Drinking Water System Components",
"category_standards_attributes"=>{
"0"=>{
"standard_id"=>"2",
"_destroy"=>"false",
"id"=>"1"
},
"new_id"=>{
"standard_id"=>""
},
"1424899001814"=>{
"standard_id"=>"1"
}
}
},
"commit"=>"Save Changes",
"id"=>"2"
}
User Load (5.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
Category Load (4.0ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = $1 LIMIT 1 [["id", "2"]]
Unpermitted parameters: 0, new_id, 1424899001814
(4.0ms) BEGIN
Category Exists (6.0ms) SELECT 1 AS one FROM "categories" WHERE ("categories"."description" = 'Drinking Water System Components' AND "categories"."id" != 2) LIMIT 1
SQL (6.0ms) UPDATE "categories" SET "description" = $1, "updated_at" = $2 WHERE "categories"."id" = 2 [["description", "Drinking Water System Components"], ["updated_at", Wed, 25 Feb 2015 21:16:44 UTC +00:00]]
It updates the description field just fine, but what's up with the Unpermitted parameters? My attributes hash comes out just like the example in the Rails Guide on nested forms, and it even says "The keys of the :addresses_attributes hash are unimportant, they need merely be different for each address." And yet it's the keys that are getting denied for me.
Where have I gone wrong? Thanks!
Figured it out, after a lot of reading. The missing piece was here.
Hashes with integer keys are treated differently and you can declare the attributes as if they were direct children. You get these kinds of parameters when you use accepts_nested_attributes_for in combination with a has_many association:
# To whitelist the following data:
# {"book" => {"title" => "Some Book",
# "chapters_attributes" => { "1" => {"title" => "First Chapter"},
# "2" => {"title" => "Second Chapter"}}}}
params.require(:book).permit(:title, chapters_attributes: [:title])
The important part of that was "Hashes with integer keys". My hash keys were passing as "0", "new_id", "1240934304343". It isn't important that I use "new_id", because that's just a placeholder value that gets changed in my jQuery when new rows are added. Only the template row retains that value, which is fine, because it gets filtered out by my reject_if clause.
But the fact that "new_id" isn't an integer apparently was the thing that was mucking it all up. So I changed it to "-1", which Rails accepts (even though it is still filtered out by reject_if, as it should be).
<div class="hidden" id="std-add-new-template">
<div class="field">
<%= f.fields_for :category_standards, CategoryStandard.new, child_index: "-1" do |ff| %>
<%= ff.collection_select :standard_id, #standards - #category.standards, :id, :number_with_exceptions, prompt: "Select a standard to add" %>
<% end %>
</div>
</div>
Your attribute keys don't seem to match what you are expecting in your strong parameters, "new_id" and "1424899001814" certainly will not be permitted.
"new_id"=>{
"standard_id"=>""
},
"1424899001814"=>{
"standard_id"=>"1"
}
I suspect the way you are constructing your form is invalid. Try breaking it down to the simplest working form.. like:
<div id="standard-list">
<%= link_to "<span class='glyphicon glyphicon-remove'></span>".html_safe, "", class: "del-std-btn", title: "Remove standard from category" %>
<div class="hidden" id="std-add-new-template">
<div class="field">
<%= f.fields_for :category_standards do |ff| %>
<%= ff.object.standard.number_with_exceptions %>
<%= ff.hidden_field :standard_id %>
<%= ff.hidden_field :_destroy %>
<%= ff.collection_select :standard_id, #standards - #category.standards, :id, :number_with_exceptions, prompt: "Select a standard to add" %>
<% end %>
</div>
</div>
</div>
The intention is to have only one nested form, and by stripping it down create only one nested hash.
"category_standards_attributes"=>{
"0"=>{
"standard_id"=>"2",
"_destroy"=>"false",
"id"=>"1"
}
}
What happens with this?

Ruby on Rails - creating a child table in a habtm relationship

I have a list of tables that can be created fine. I want to then be able to create a child table by clicking Edit on the parent (goes to edit.html.erb) and then clicking 'New child'. The relationship between the parent and child tables is has_and_belongs_to_many.
<%= button_to 'New Child', new_parent_child_path([#parent, #parent.children.build]), :method => :get %>
This is the link to the new child form, producing the following error:
'undefined method `children' for nil:NilClass'
The server log gives me the following:
Started GET "/parents/1//children/new" for blah at blah
Processing by ChildrenController#new as HTML
Parameters: {"parent_id"=>"1"}
Rendered children/_form.html.erb (1.3ms)
Rendered children/new.html.erb within layouts/application (1.9ms)
Completed 500 Internal Server Error in 4ms
ActionView::Template::Error (undefined method `children' for nil:NilClass):
1: <%= form_for ([#parent, #parent.children.build]) do |f| %>
2:
3: <div class = "field">
4: <%= f.label :Child_name %><br/>
app/views/children /_form.html.erb:1:in`_app_views_children__form_html_erb__2140243687_70034946643120'
app/views/children/new.html.erb:3:in `_app_views_children_new_html_erb__655166082_70034947829100'
The _form.html.erb that renders in the child new.html.erb is as follows:
<%= form_for ([#parent, #parent.children.build]) do |f| %>
<div class = "field">
<%= f.label :Child_name %><br/>
<%= f.text_field :ChildName %>
</div>
<div class = "field">
<%= f.label :Child_email_address %><br/>
<%= f.text_field :ChildEmail %>
</div>
<div class = "actions">
<%= f.submit %>
</div>
<% end %>
(Note: the tables are not actually called parent and child, I just used those names for secrecy's sake. Also, please don't advise me to use has_many through, I'm not interested.)
Edit #1: Code from 'ChildrenController'
def new
#child = Child.new
end
def create
#parent = Parent.find(params [:parent_id])
#scout = #parent.children.build(params[:child])
redirect_to parent_path(#parent)
end
You need to include #parent = Parent.find(params[:parent_id]) in the new action because you are using it in the new.html.erb template. Otherwise it will be nil and #parent.children.build will not work.

Rails nested resource on a singular resource form

I'm having a problem with Rails not POSTing anything in the params to an action.
I'm using a singular resource with a nested plural resource which may or may
not be where the problem is coming from (Rails has issues with form_for and singular
resource URLs).
Anyway, I have this in my routes:
resource :event do
resources :actions, :only => [:create], :controller => "events/actions"
end
The view:
<%= form_for([#event, Action.new], :remote => true) do |f| %>
<div class="field">
<%= f.label :team_id %>
<br />
<%= f.text_field :team_id %>
</div>
<div class="field">
<%= f.label :message %>
<br />
<%= f.text_field :message %>
</div>
<div class="field">
<%= f.label :score %>
<br />
<%= f.number_field :score %>
</div>
<br />
<%= f.submit "Update score" %> or <%= link_to "cancel", "#", :id => "cancel" %>
<% end %>
The create action:
def create
#event = Event.find(params[:event_id])
#action = #event.actions.create(params[:action])
end
Ok pretty standard no worries there.
But when I get the params from Rails nothing is there. :(
Params:
Started POST "/event/actions.4e67f09349ae71090c00000e"
Processing by Events::ActionsController#create as
Parameters: {"utf8"=>"Γ£ô", "authenticity_token"=>"stuff", "commit"=>"Update score"}
Completed 500 Internal Server Error in 31ms
What is going on here?
Edit:
If I remove the ":remote => true" line in my view,
I see that in my params I get one param ":format"
which appears to be the ID of the event.
However, I'm still not getting the action params. :(
Ideally I'd like to see those event & action models - I suspect that's where the problem lies. Without seeing those, a few suggestions:
Is 'accepts_nested_attributes_for :action' set in the event model?
Remove any 'attr_accessible' line from both models & see if things work. (Keep in mind you need to set accessible attributes for nested forms in the parent model)
'Action' seems like an imprudent name for a model. It's possible rails is overwriting 'action' methods with things related to the actual action
Hope this helps - I'd suggest posting the models if you still can't find a solution.

Resources