Rails - undefined method `each' for 0:Fixnum - ruby-on-rails

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.

Related

Rails Cocoon read only fields

I am using Cocoon for nested forms. For task records that are already created, I want description to be read_only.
projects/_form::
= simple_form_for #project do |f|
= f.input :name
= f.input :description
%h3 Tasks
#tasks
= f.simple_fields_for :tasks do |task|
= render 'task_fields', f: task
.links
= link_to_add_association 'add task', f, :tasks
= f.submit
And _task_fields :
.nested-fields
- if #task.description?
= f.text_field :description, disabled: true
- else
= f.input :description
= f.input :done, as: :boolean
= link_to_remove_association "remove task", f
Currently the validation in _task_fields is not working: undefined method <description> for nil:NilClass in the line with if statement. How can I write the IF statement correctly?
For task records that are already created, I want description to be
read_only.
You are doing it wrong. You should be able do the check by using new_record? like so
.nested-fields
- unless f.object.new_record?
= f.text_field :description, disabled: true
- else
= f.input :description
= f.input :done, as: :boolean
= link_to_remove_association "remove task", f
Also, by using disabled: true, the value of description won't be submitted and cannot be passed through params. If you want to have the description value in the params, use readonly: true instead.
.nested-fields
- unless f.object.new_record?
= f.text_field :description, readonly: true
- else
= f.input :description
= f.input :done, as: :boolean
= link_to_remove_association "remove task", f

Ruby on Rails: Calling Model method in view

I am trying to achieve Paypal integration with rails. Following this (http://railscasts.com/episodes/141-paypal-basics) I have a function in model which call paypal service with a return url. I have added a link in view that links to method in model.But some how rails is not able to get function in model.
What am i doing wrong?
My View :
form_for #order do |f|
- if #order.errors.any?
#error_explanation
h2 = "#{pluralize(#order.errors.count, "error")} prohibited this order from being saved:"
ul
- #order.errors.full_messages.each do |message|
li = message
.field
= f.label :first_name
= f.text_field :first_name
.field
= f.label :last_name
= f.text_field :last_name
.field
= f.label :card_number
= f.text_field :card_number
.field
= f.label :card_verification, "Card Verification Value (CVV)"
= f.text_field :card_verification
.field
= f.label :card_expires_on
= f.date_select :card_expires_on, {start_year: Date.today.year, end_year: (Date.today.year+10), add_month_numbers: true, discard_day: true}, {class: "browser-default"}
.field
= link_to 'PayPal', #order.paypal_url(current_user)<= link to model function
.actions
= f.submit
My Model : defined following function.
def paypal_url(return_url)
values = {
:business => 'xxxxx.XXXXX#techflex.com',
:cmd => '_cart',
:upload => 1,
:return => return_url,
:invoice => id
}
values.merge!({
"amount_#{1}" => item.unit_price,
"item_name_#{1}" => item.product.name,
"item_number_#{1}" => item.id,
"quantity_#{1}" => item.quantity
})
"https://www.sandbox.paypal.com/cgi-bin/webscr?" + values.to_query
end
Error :
NoMethodError in Orders#new
Showing C:/Users/Suniljadhav/SourceCode/TrainStation/app/views/orders/_form.html.slim where line #24 raised:
private method `paypal_url' called for #<Order:0x5e49bf8>
Trace of template inclusion: app/views/orders/new.html.slim
Rails.root: C:/Users/Suniljadhav/Source Code/TrainStation
It seems that paypal_url is a private method and you are trying to call it from outside of its class.
In your Order class you probably have the keyword private. Every method below that keyword will be declared private (as opposed to public) and an error will be thrown if you try to call it from outside the class. Private methods can only be called from within the class where they are defined. So try moving the definition of paypal_url so that it appears before private in your class.
You can read more about how this works here, and about the reasons behind it here.

Passing params to a partial in slim

Rails 3.2
Ruby 2.15
I have a somewhat complex view app/views/tickets/show.html.slim. Inside this view, I render various sections of the the ticket
One section is called customer_info. Here's what I have:
render 'tickets/sections/customer_info', locals: { customer_info: CustomerInfo.new, ticket: #ticket }
In my app/views/tickets/sections/_customer_info.html.slim, I have:
= form_for(customer_info) do |f|
- :ticket_id = ticket.id
= f.hidden_field :ticket_id
.form-horizontal-column.customer-info
.form-group
= f.label :first_name
= f.text_field :first_name
.form-group
= f.label :last_name
= f.text_field :last_name
.actions = f.submit 'Save'
.clear
I am however getting the following error message:
_customer_info.html.slim:2: syntax error, unexpected '=', expecting keyword_end
; :ticket_id = ticket.id;
^
I am starting to learn .slim. Any ideas?
You can't set a symbol to something - they are immutable. You can remove the 2nd line do do something like:
= form_for(customer_info) do |f|
= f.hidden_field :ticket_id, :value => ticket.id
...

Unable to save data add to the model created with Devise (name model : user )

I use currency with ruby rails 4, I add fields in my user table I created with currency and the concern is when I want to change my email it works but if I want to update the other fields nothing happens so I do not know what 'would forget.
Here the form of currency or I add the fields.
%h2
Edit #{resource_name.to_s.humanize}
= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f|
= devise_error_messages!
%div
= f.label :email
%br
= f.email_field :email, :value =>"#{current_user.email}"
%div
= f.label 'Mot de passe'
%i (Pour valider votre profil)
%br
= f.password_field :current_password
%div
= f.label 'Nom'
%br
= f.text_field :username, :value=>"#{current_user.username}"
%div
= f.label 'Prenom'
%br
= f.text_field :firstname, :value=>"#{current_user.firstname}"
%div
= f.label 'Adresse'
%br
= f.text_field :adress, :value=>"#{current_user.adress}"
%div
= f.label 'Code postal'
%br
= f.text_field :cp, :value=>"#{current_user.cp}"
%div
= f.label 'Ville'
%br
= f.text_field :city, :value=>"#{current_user.city}"
%div= f.submit "Mise à jour du profil"
To update the custom fields that you added to Devise model, you will have to permit them explicitly:
Add the following code in your ApplicationController
class ApplicationController < ActionController::Base
before_filter :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
## Permit the custom fields below which you would like to update, I have shown a few examples
devise_parameter_sanitizer.for(:account_update) << :currency << :username << :firstname
end
end

Rails, MongoMapper nested Array in HAML form trouble

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'

Resources