Rails 3.2.12 and Ruby 1.9.3 and Haml
I would like to use the count of attribute to control the display of a 'link_to "remove"', but I am having problems with setting up the logic.
Following is some code from my form as it is currently:
.field
= codeline.label :name, "Units Alloc"
%br/
= codeline.text_field :units_alloc, :precision => 6, :scale => 2, :size => 10,
:class => "ui-state-default"
= codeline.hidden_field :_destroy
= link_to "remove", '#', class: "remove_fields"
this works well but I have the 'remove' link showing up and I would prefer it to only show if there are two :units_alloc attributes.
This is what I tried:
.field
= codeline.label :name, "Units Alloc"
%br/
= codeline.text_field :units_alloc, :precision => 6, :scale => 2, :size => 10,
:class => "ui-state-default"
- if :units_alloc.count > 1
= codeline.hidden_field :_destroy
= link_to "remove", '#', class: "remove_fields"
and here is my error:
NoMethodError in Contracts#new
Showing /home/tom/rails_projects/tracking/app/views/contracts
/_codeline_fields.html.haml where line #9 raised:
undefined method `count' for :units_alloc:Symbol
if I use units_alloc in the argument instead of the symbol, I still get an error:
NameError in Contracts#new
Showing /home/tom/rails_projects/tracking/app/views/contracts
/_codeline_fields.html.haml where line #9 raised:
undefined local variable or method `units_alloc' for
#<#<Class:0xadbde90>:0xa8956e8>
I tried to use 'codeline.units_alloc' but this did not work and the same error was flagged.
Any suggestions, or pointers to help me resolve this issue?
Thanks.
Solution: Thanks to James Scott Jr.
app/controller/contracts_controller.rb
def New
#show_remove = false
....
....
end
app/views/contracts/_codelines_fields.html.haml
.field
= codeline.label :name, "Units Alloc"
%br/
= codeline.text_field :units_alloc, :precision => 6, :scale => 2, :size => 10,
:class => "ui-state-default"
- if #show_remove
= codeline.hidden_field :_destroy
= link_to "remove", '#', class: "remove_fields"
- else
- #show_remove = true
And that did it ... the remove button only shows in the second and subsequent row of attributes.
By the time you're in the form (partial), codeline doesn't refer to an instance the instance of Codeline that the form (partial) is for, but an instance of an ActionView::Helpers::FormBuilder that simple knows how to associate information the the instance of Codeline. You knew that because in the first line of the partial, you have codeline.object.build_code.
So, if you want to access the information about the units_alloc associated, you would access them with codeline.object.units_alloc. That will give you your data for your conditional.
I would just like to add that if the purpose of your anchor tag is to remove elements from a form list using some javacscript, you might be using the wrong control for it. Anchor tags are not form elements, they should point to resources/content and are not there to be used as animation/client side behaviour triggers. According to the use case you describe, an input tag type=button would be a much more appropriated element for what you seem to be trying to achieve.
Related
I have an APEX page with a few select lists, using collections to save and refresh the page. The "System" select list is dependent upon the "Brand" selected value. I have the page working how I want, but it requires the page to be submitted after a "Brand" value is chosen so that the "System" lov is properly populated.
I am trying to make it so that when the "Brand" value is changed or selected, the "System" lov is changed immediately without needing a page submit. I've been reading and it seems you need to use jQuery and AJAX callback, but I just cannot get my head around that and make any progress.
Hoping someone can help me. I am certainly a rookie with web coding in general.
Added: my local APEX version is 5.0.3.
https://apex.oracle.com/pls/apex/f?p=4550
Workspace: UNCLE_BUCK_WS
User: developer1
Pwd: developer1
Application 83513 - Tooth Selector
Page 5: Page that works using submit page
Page 6: Page I started trying to use jQuery, but did not get very far
Added:
There is no cascading LOV for manual tabular form. See my region definition below. Hoping someone can still help, maybe copy my page and make the required changes.
select apex_item.select_list_from_query(p_idx => 1,
p_value => n001,
p_query => 'SELECT tooth_number display_value, tooth_number return_value FROM psp_teeth WHERE tooth_numbering_method = :P5_TOOTH_NUMBER_METHOD
ORDER BY tooth_number',
p_null_text => '- Choose Tooth -')
|| apex_item.hidden(p_idx => 50, p_value => seq_id) Tooth_Number
, apex_item.select_list_from_lov(p_idx => 10, p_value => c001, p_lov => 'BRAND', p_null_text => '- Choose Brand -') brand
, apex_item.select_list_from_lov(p_idx => 12, p_value => c002, p_lov => 'TYPE', p_null_text => '- Choose Type -') type
, apex_item.select_list (p_idx => 14,
p_value => c003,
p_list_values => CASE c001
WHEN 'BRAND_A' THEN 'SYSTEM_A1,SYSTEM_A2'
WHEN 'BRAND_B' THEN 'SYSTEM_B1,SYSTEM_B2'
END,
p_show_null => 'YES',
p_null_text => '- Choose System -') system
, apex_item.checkbox2(p_idx => 16, p_value => seq_id, p_checked_values => CASE c004 WHEN '1' THEN seq_id END) delete_row
from apex_collections
where collection_name = 'TEMPCOL'
Don't use tabular forms, they are archaic! Use an Interactive Grid instead, which supports cascading LOVs declaratively (you don't need to write any code, just specify that column TYPE cascades from column BRAND):
See page 9 in your app where I have created one (I didn't bother creating the master region). My LOV definition for TYPE is a bit cumbersome, you may be able to improve on that (i.e. get back to the "dynamic static" LOV that you had before).
I have a simple spec testing the creation of an object of the Baseline class.
it "allows a user to create a baseline score with valid content" do
expect(#user.baselines.count).to eq(0)
#baseline = post(:create, :user_id => #user.id, :baseline => valid_attributes)
expect(response).to redirect_to '/patients/list'
expect(flash[:notice]).to eq("Baseline scores for case #{#baseline.case_id} was successfully created.")
expect(Baseline.all.count).to eq(1)
end
But I get this. I am uncertain where to begin with this - I am uncertain why I can't access the case_id attribute of #baseline.
NoMethodError:undefined method `case_id' for <ActionController::TestResponse:0x007f8f5ab4f3c0>
Just to show...these are the valid attributes
let(:valid_attributes) do {
:dx1 => "IPF",
:dxcon1 => 100,
:db1 => "Progressive",
:dbcon1 => 100,
:mgt=> "Drugs",
:biopsy => "Yes",
:patient_id => #patient.id,
:case_id => #patient.case,
}
end
post doesn't return a model instance it returns a TestResponse object which gives you access to headers, status code, etc. To access the object created as a side effect of calling the :create action you can do Baseline.last (in this case Baseline.first would also work since there are no existing baseline objects)
Also note - if you have an instance variable named #baseline that is assigned in the controller you can access that with assigns(:baseline)
expect(assigns[:baseline]).to be_a(Baseline)
I am getting a pesky ActiveModel::ForbiddenAttributesError, and I can't find how to fix it.
I was following this guide:
https://rbudiharso.wordpress.com/2010/07/07/dynamically-add-and-remove-input-field-in-rails-without-javascript/
and everything works great except when I am updating the record.
My controller:
if params[:add_item]
# rebuild the baixa_itens attributes that doesn't have an id
unless params[:baixa][:baixa_itens_attributes].blank?
for attribute in params[:baixa][:baixa_itens_attributes]
#baixa.baixa_itens.build(attribute.last.except(:_destroy)) unless attribute.last.has_key?(:id)
end
(...)
# Never trust parameters from the scary internet, only allow the white list through.
def baixa_params
params.require(:baixa).permit(:prontuarios_id, :data, :profissional_id, :comentario, baixa_itens_attributes: [ :id, :item_id, :quantidade, :preco, :_destroy ])
end
end
When I got my crash, I tested it in the console:
>> attribute
=> ["1", {"item_id"=>"3", "quantidade"=>"1", "preco"=>"1", "_destroy"=>"0"}]
>> attribute.last.except(:_destroy)
=> {"item_id"=>"3", "quantidade"=>"1", "preco"=>"1"}
>> #baixa.baixa_itens.build(attribute.last.except(:_destroy))
!! #<ActiveModel::ForbiddenAttributesError: ActiveModel::ForbiddenAttributesError>
But if I try to make this build MANUALLY, it works:
>> #baixa.baixa_itens.build({"item_id"=>"3", "quantidade"=>"1", "preco"=>"1"})
=> #<BaixaItem id: nil, baixa_id: 6, item_id: 3, quantidade: 1, preco: #<BigDecimal:7ff97147b6f8,'0.1E1',9(18)>, created_at: nil, updated_at: nil>
>>
What am I missing?
I workaround this problem doing
for attribute in params[:baixa][:baixa_itens_attributes].permit!
in my controler (not using ! would give me error: "Unpermitted parameters: 0, 1", which i cant understand)
I still wish a better solution.
I have radiobuttons creaated in loop.
- #annual_packages.each do |plan|
= f.radio_button('plan_id', plan.id)
= plan.title
How can I make condition to check radio if plan.id == #plan.id
Doesn't work:
= f.radio_button('plan_id', plan.id, checked: (plan.id == #plan.id))
Loop code:
= form_for #organization, url: subscription_create_path, remote: true, method: :post do |f|
- unless #annual_packages.blank?
- #annual_packages.each do |plan|
= f.radio_button('plan_id', plan.id)
= plan.title
Can you post your form_for code?
Cause if your column plan_id have any value, it should be checked automatically if it matches the tag_value`.
If the current value of method is tag_value the radio button will be
checked.
http://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-radio_button
If you are using simple_form or form_for, and f is your form object then you can simply do:
for form_for:
radio_button(object_name, method, tag_value, options = {}) refer
in your case:
- #annual_packages.each do |plan|
radio_button("your object name", "plan_id", plan.id, options = {})
If the current value of method(object.plan_id) is tag_value(plan) the radio button will be checked.
for simple_form:
= f.input :plan_id, as: :radio_buttons, collection: #annual_packages
I am creating google charts to show Google Analytics data from the past 7 days. I have an issue with the X-Axis labels stacking on top of each other when I have certain data (or at least that's all I can tell is different.)
I am generating the API call using this gem: https://github.com/mattetti/googlecharts and I've looked at what each part of the URL is doing and can't find the issue, but I'm sure I'm missing something.
Here is an example of two sites data over the same time period, the first one shows the issue and the second one is a working example:
Here is the URL, these are text encoded for readability, but it has the same issues when switched to simple or extended encoding:
BROKEN VERSION:
https://chart.apis.google.com/chart?chxl=0:|11-22|11-23|11-24|11-25|11-26|11-27&chxt=x&chco=58838C,BF996B,BF5841,A61C1C&chf=bg,s,ffffff&chd=t:979,807,681,653,580,509|822,724,602,562,519,455|540,409,381,375,336,301|307,156,173,176,155,133&chds=0,979&chdl=Visits|Visitors|New+Visits|Organic+Searches&chtt=Google+Analytics+-+Last+7+Days&cht=lc&chs=600x200&chxr=0,979,979|1,822,822|2,540,540|3,307,307
WORKING VERSION:
https://chart.apis.google.com/chart?chxl=0:|11-22|11-23|11-24|11-25|11-26|11-27&chxt=x&chco=58838C,BF996B,BF5841,A61C1C&chf=bg,s,ffffff&chd=t:1385,1395,981,947,863,731|1083,1222,832,715,690,546|580,566,427,413,387,329|247,151,151,171,162,135&chds=0,1395&chdl=Visits|Visitors|New+Visits|Organic+Searches&chtt=Google+Analytics+-+Last+7+Days&cht=lc&chs=600x200&chxr=0,1385,1395|1,1083,1222|2,580,580|3,247,247
The chxr values were incorrect. The gem was generating them for multiple axes when it should have only been generating them for one. I manually overrode the min, max and step in the gem and it worked.
Here is my code using the gem, first getting the max value from all my data points:
[#visits,#visitors,#new_visits,#organic_searches].each do |array|
array.values.each do |value|
#max_value = value if (value > #max_value)
end
end
# Chart it
chart = Gchart.line(
:title => prop.to_s.upcase + ' Google Analytics - Past 7 Days',
:size => '600x200',
:bg => 'ffffff',
:axis_with_labels => ['x'],
:axis_labels => [#visits.keys],
:legend => ['Visits','Visitors','New Visits','Organic Searches'],
:line_colors => ['58838C','BF996B','BF5841','A61C1C'],
:encoding => 'text',
:data => [#visits.values,#visitors.values, #new_visits.values, #organic_searches.values],
:max_value => #max_value,
:axis_range => [nil, [0, #max_value, (#max_value / 10).to_i]],
:format => 'image_tag')