I'm getting
undefined method `Carrots' for # (referencing ln#18)
When trying to edit with the below form:
= form_for #harvest do |f|
- if #harvest.errors.any?
#error_explanation
%h2= "#{pluralize(#harvest.errors.count, "error")} prohibited this harvest from being saved:"
%ul
- #harvest.errors.full_messages.each do |msg|
%li= msg
.field
= f.label :created_at
= f.text_field :created_at, :disabled => true
%br
= f.label :photo
= f.text_field :photo
%h2 Crops
- #harvest.harvested_crops.each do |harvested_crop|
= f.label :harvested_crop['crop']
= f.select harvested_crop['crop'], Crop.all.collect {|p| [ p.name, p.id ] }, {:include_blank => ''}
= f.label :harvested_crop['amount']
= f.text_field harvested_crop['amount']
%br
.actions
= f.submit 'Save'
Using the data below:
{ "_id" : ObjectId("5067846f37bca62bccc3729e"), "user_id" : "5067844637bca62bccc3729c", "photo" : "carrotsnspuds.jpg", "harvested_crops" : [ { "crop" : "Carrots", "amount" : 1112.15 }, { "crop" : "Potatoes", "amount" : 3212.44 } ] }
I've tried related Stack Overflow questions for MongoMapper, Rails and Embedded documents but I am not having any luck, perhaps due to this being a nested Array rather than EmbeddedDocument. I'm not using Formtastic or anything yet, would just like to understand the syntax required here first.
This is definitely not efficient, but this allowed me to get the job done:
= form_for #harvest do |f|
- if #harvest.errors.any?
#error_explanation
%h2= "#{pluralize(#harvest.errors.count, "error")} prohibited this harvest from being saved:"
%ul
- #harvest.errors.full_messages.each do |msg|
%li= msg
.field
= f.label :created_at
= f.text_field :created_at, :disabled => true
%br
= f.label :photo
= f.text_field :photo
%h2 Crops
- x = 0
- #harvest.harvested_crops.each do |harvested_crop|
= f.fields_for "harvested_crops[]", harvested_crop do |hc|
%b Harvested Crop
%select{:name => "harvest[harvested_crops][" + x.to_s + "][crop]"}
- Crop.all.collect.each do |crop_name|
- if harvested_crop['crop'] == crop_name[:name]
%option{:selected => "selected", :value => crop_name[:name]}
= crop_name[:name]
- else
%option{:value => crop_name[:name]}
= crop_name[:name]
%b Amount
%input{:name => "harvest[harvested_crops][" + x.to_s + "][amount]", :value => harvested_crop['amount']}/
%br
- x += 1
%br
.actions
= f.submit 'Save'
Related
I have a view associated with one model but there are multiple versions of the same form that are hidden until a jquery function shows them. When I try to submit one, all the fields are empty.
Here is the view in question:
= form_for #rfi do |f|
- if #rfi.errors.any?
#error_explanation
h2 = "#{pluralize(#rfi.errors.count, "error")} prohibited this rfi from being saved:"
ul
- #rfi.errors.full_messages.each do |message|
li = message
.field
= f.label :svg_ref, "SVG PO Number"
= f.text_field :svg_ref
.field
= f.label :vendor_ref, "Vendor SO Number"
= f.text_field :vendor_ref
.field
= f.label :due
= f.text_field :due
= f.hidden_field :rfi_type, value:"order"
.actions
= f.submit
.rfi_type.rfi_type_quote
= form_for #rfi do |f|
- if #rfi.errors.any?
#error_explanation
h2 = "#{pluralize(#rfi.errors.count, "error")} prohibited this rfi from being saved:"
ul
- #rfi.errors.full_messages.each do |message|
li = message
.field
= f.label :reference, "Quote number"
= f.text_field :reference
= f.hidden_field :rfi_type, value:"quote"
.field
= f.label :due
= f.text_field :due
.actions
= f.submit
This is the Jquery involved
$ ->
$(".rfi_type").hide()
$(".rfi_type_order").show()
$("input:radio[name=rfi_type]").change ->
$(".rfi_type").hide()
$(".rfi_type_"+$(this).val()).show()
return
return
I believe 'one' of the problems you may be facing is your use of the hidden_field with the same name. I would suggest you restructure both your haml and coffee-script to something like this
$ ->
$("#quote-fields").hide()
$("input:radio[name=rfi_type]").change ->
$(".show-hide-container").hide()
type = $(this).val()
$("#" + type + "-fields" ).show()
$("#rfi_type-field").val(type)
= form_for #rfi do |f|
- if #rfi.errors.any?
#error_explanation
h2 = "#{pluralize(#rfi.errors.count, "error")} prohibited this rfi from being saved:"
ul
- #rfi.errors.full_messages.each do |message|
li = message
#order-fields.show-hide-container
.field
= f.label :svg_ref, "SVG PO Number"
= f.text_field :svg_ref
.field
= f.label :vendor_ref, "Vendor SO Number"
= f.text_field :vendor_ref
#quote-fields.field.show-hide-container
= f.label :reference, "Quote number"
= f.text_field :reference
.field
= f.label :due
= f.text_field :due
= f.hidden_field :rfi_type, value: "order", id: "rfi_type-field"
.actions
= f.submit
This code
new_hash = [{:id=>1,:name_t=>"Y/N"},{:id=>2,:name_t=>'Number'},{:id=>3,:name_t=>'Text'}]
collection_select(:question,:q_type, new_hash ,:id,:name_t )
is throwing the error
undefined method ``name_t' for {:id=>1, :name_t=>"Y/N"}:Hash
And is driving me crazy
Update
I'm trying to add a DRY static dropdown menu to a form
= form_for #question do |f|
- if #question.errors.any?
#error_explanation
%h2= "#{pluralize(#question.errors.count, "error")} prohibited this question from being saved:"
%ul
- #question.errors.full_messages.each do |msg|
%li= msg
.row{:style=>"margin-bottom: 5px"}
.col-xs-2
= f.label :pm_type
.col-xs-2
= f.collection_select(:pm_type_id, PmType.all.where(:is_active => true), :id, :name)
.row{:style=>"margin-bottom: 5px"}
.col-xs-2
= f.label :proposition
.col-xs-2
= f.text_field :proposition
.row{:style=>"margin-bottom: 5px"}
.col-xs-2
= f.label "Type"
.col-xs-2
-# ######## Code goes here ##########
- new_hash = [{:id=>1,:name_t=>"Y/N"},{:id=>2,:name_t=>'Number'},{:id=>3,:name_t=>'Text'}]
= collection_select(:question,:q_type, new_hash ,:id,:name_t )
.row{:style=>"margin-bottom: 5px"}
.col-xs-2
= f.label "Weight"
.col-xs-2
= f.text_field :q_weight
.row{:style=>"margin-bottom: 5px"}
.col-xs-2
= f.label "Active?"
.col-xs-2
= check_box_tag(:is_active, 1 ,true)
.actions
= f.submit 'Save'
According to the documentation here the fifth parameter of collection_select should be a method.
From the api-doc:
The :value_method and :text_method parameters are methods to be called on each member of collection.
Since name_t is a key of the element in the array you cannot use it as a method.
But you can try this:
class MyType < Struct.new(:id, :name_t)
end
new_hash = [MyType.new(1,"Y/N"),MyType.new(2,"Number"),MyType.new(2,"Text")]
p new_hash[0].name_t
Thanks for the help! i found the select method while looking at the documentation, and end up doing the following:
select(:question,:q_type, [["Y/N",1],['Number',2],['Text',3]], {} )
app/views/users/_form.html.haml, line 10
= form_for #user do |f|
- if #user.errors.any?
#error_explanation
%h2= "#{pluralize(#user.errors.count, "error")} prohibido que este usuario se guarde:"
%ul
- #user.errors.full_messages.each do |msg|
%li= msg
.field
= f.label 'Usuario'
= f.text_field :username
.field
= f.label :email
= f.text_field :email
.field
= f.label 'Teléfono'
= f.phone_field :phone
.field
= f.label 'Contraseña'
= f.password_field :password
.field
= f.label 'Reingresar Contraseña'
= f.password_field :password_confirmation
.control-group
//= f.label 'Permisos'
%ul.unstyled
- for role in Role.find(:all)
%li
= check_box_tag "user[role_ids][]", role.name, #user.roles.include?(role)
= role.name
%br
.actions
= f.submit 'Guardar',:class => 'btn btn-primary'
Error seems to be in the = f.label I've deleted all the = f.label and no errors were thrown, also changed = f.label 'Usuario' for = f.label :username with no luck.
Error Message:
ArgumentError in Users#new
Showing C:/Sites/AutosCostaRica/app/views/users/_form.html.haml where line #10 raised:
syntax error in "<reader>", line 3, column 18:
next_label: >>
^
Update:
I just found out that it works perfect on Ruby 1.8, but I waned to be 1.9.
Any ideas?
I get the following error in Ruby on Rails undefined method 'each' for 0:Fixnum.
Here is the application trace :
app/controllers/videos_controller.rb:23:in `new'
app/controllers/videos_controller.rb:23:in `create'
And my controller create and new actions :
def new
#video = Video.new
end
def create
method = 'get_' + params[:video][:provider] + '_video_id'
params[:video][:provider_video_id] = Video.send(method, params[:video][:url])
params[:video][:thumb] = Video.get_thumb_from_youtube(params[:video][:provider_video_id])
params[:video][:views] = params[:video][:likes] = 0
params[:video][:user_id] = current_user
#video = Video.new(params[:video])
if #video.save
redirect_to video_path(#video), notice:'Video added successfully.'
else
render :new
end
end
Here is my view.html.haml :
= form_for #video do |f|
- if #video.errors.any?
.error_explanation
%h2= pluralize(#video.errors.count, "error")
prohibited this user from being saved:
%ul
- #video.errors.full_messages.each do |msg|
%li= msg
.field
= f.label :title
= f.text_field :title
.field
= f.label :description
= f.text_area :description
%br
.field
= f.label :url, 'URL'
= f.text_field :url
%br
.field
Provider
= radio_button :video, :provider, 'vimeo'
= f.label :provider, 'Vimeo', :value => 'vimeo'
= radio_button :video, :provider, 'youtube'
= f.label :provider, 'Youtube', :value => 'youtube'
%br
.field
Category
= collection_select(:video, :category_id, Category.all, :id, :name, :include_blank => true)
%br
.actions
= f.submit "Add video"
From
params[:video][:views] = params[:video][:likes] = 0
to
params[:video][:views] = 0
I assume that video.likes is an association, not a count, so it expects an Enumerable. If it's an association, rails tries to assign the elements you add to likes to your video model. The first step of adding them is to iterate - using each. That's where the error comes from.
Suddenly my Rails application acts weirdly. If I attach an image on a edit page and submit the form, show action is called instead of update action. If I don't attach any image, it's ok.
Routes
namespace :admin do
resources :products do
collection do
get :download_images
get :all_styles
post :compress_images
end
end
end
View
%h1 Edit Product
= form_for [:admin, #product], :html => {:multipart => true} do |f|
- if f.object.accessory?
= render 'form_accessory', :f => f
- else
= render 'form', :f => f
[_form partial]
:javascript
function exclusiveCheck(check_box) {
if (check_box.checked) {
$('#pictures_fieldset .is_main').attr('checked', false);
check_box.checked = true;
}
}
- error_messages_for f.object
%fieldset.span-23
%legend
.row
= f.label :category_id, :class => "required"
%br
= f.select :category_id, array_of_categories('garment'), :include_blank => true
= f.check_box :new_in_category
= image_tag "new_icon.png"
.row
= f.label :style_no, :class => "required"
%br
= f.text_field :style_no, :style => "width: 5em;"
.row
= f.label :name, :class => "required"
%br
= f.text_field :name
.row
= f.label :fabric_info, :class => ""
%br
= f.text_field :fabric_info, :style => "width: 90%;"
.row
= f.label :lb_per_piece, "Weight", :class => "required"
%br
= f.text_field :lb_per_piece, :style => "width: 3em;"
.row
.span-3
= f.label :price, :class => "required"
%br
= f.text_field :price, :style => "width: 6em;"
.span-3
= f.label :original_price, :class => ""
%br
= f.text_field :original_price, :style => "width: 6em;"
.span-10
%label Discount Rate
%br
= text_field_tag :percent, nil, :style => "width: 3em;", :id => "percent"
= "%"
= link_to_function "Set", "setPrice();"
= f.check_box :new_in_sale
= image_tag "new_icon.png"
.row
= f.label :video, :class => ""
%br
= f.text_field :video, :style => "width: 15em;"
(Upload videos to data.ohyesfashion.com at ~/files/videos directory.)
.row
= f.label :available_on
%br
= f.text_field :available_on, :class => 'date'
.row
= f.check_box :active
= f.label :active, :class => ""
.row
= f.check_box :new_arrival
= f.label :new_arrival, :class => ""
= f.check_box :new_in_new_arrival?
= image_tag "new_icon.png"
.row
= f.check_box :best
= f.label :best, :class => ""
= f.check_box :new_in_best
= image_tag "new_icon.png"
.span-24.last
%button.button.positive(type="submit")
= icon_for(:accept, "Save")
%fieldset
%legend Sizes
- Product::SIZES.each do |size|
.span-2
= size.to_s.upcase
= f.select size, [*0..20].map { |i| i.to_s }
.span-24
.span-12
%fieldset
%legend Descriptions
- Description.order("code, name").each do |description|
.row
= check_box_tag "descriptions[]", description.id, f.object.descriptions.include?(description)
= "[#{description.code}]"
= description.name
.row
New Description
= text_field_tag :new_description_name
.span-12.last
%fieldset(style="float: right;")
%legend Colors
.span-11
- Color.order("name").each do |color|
.row
.span-1
= check_box_tag "colors[]", color.id, f.object.colors.include?(color)
.span-4
= color_box color
.span-3
= check_box_tag "sold_out_colors[]", color.id, (f.object.colors.include?(color) and Colorship.sold_out?(f.object, color))
Sold Out
.span-2
= check_box_tag "is_new_colors[]", color.id, (f.object.colors.include?(color) and Colorship.is_new?(f.object, color))
New
.span-24.last
%button.button.positive(type="submit")
= icon_for(:accept, "Save")
- (15 - f.object.pictures.size).times { f.object.pictures.build }
%fieldset#pictures_fieldset
%legend Images
.row
= f.label :model_id, "Model"
= f.select :model_id, PhotoModel.all.map { |i| [i.name, i.id] }, :include_blank => true
%br
= f.fields_for :pictures do |ff|
.span-7(style="height: 150px;")
- if ff.object.new_record?
= ff.file_field :image
- else
= image_tag ff.object.image.url(:w45)
%br
= ff.object.image_file_name
%br
= ff.check_box :is_main, :onclick => "exclusiveCheck(this)", :class => "is_main"
Title
%br
= ff.check_box "_destroy"
Delete
.span-24.last
= link_to icon_for(:back, "Back"), :back, :class => "button negative"
%button.button.positive(type="submit")
= icon_for(:accept, "Save")
:javascript
function setPrice() {
if ($('#product_original_price').val() != '' && $('#percent').val() != '') {
var originalPrice = parseFloat($('#product_original_price').val());
var percent = parseFloat($('#percent').val());
$('#product_price').val(originalPrice * (100 - percent) / 100.0);
}
}
Any idea what's going on? It used to work fine.