I have this code in my update action of Registration controller. And I get undefined method 'errors'. I can not use flash message here for some reason.
if subjects_selected.blank?
#registration = Registration.where(:student_id => params[:registration][:student_id], :semester_id => params[:registration][:semester_id] )
redirect_to editreg_registrations_path(#registration.first.id, params[:registration][:student_id], params[:registration][:semester_id]), #registration.errors.add(:You_have_to_register_for_at_least_one_subject) and return
end
How can I access error method here?
you can take the method in Registration model for error displaying the errors
like
error_array = Registration.validate_subjects(params[:registration][:student_id],params[:registration][:semester_id])
then in Registration model
def validate_subjects(student_id, semester_id)
is_registration = self.where(:student_id=>student_id,:semester_id =>semester_id)
error_array=[]
if !is_registration
//RIGHT THE CODE
error_array << "You_have_to_register_for_at_least_one_subjec"
end
error_array
end
if subjects_selected.blank?
#registration = Registration.where(:student_id => params[:registration][:student_id], :semester_id => params[:registration][:semester_id] )
if !#registration.errors
redirect_to editreg_registrations_path(#registration.first.id, params[:registration][:student_id], params[:registration][:semester_id])
else
#registration.errors.add(:You_have_to_register_for_at_least_one_subject)
end
end
Related
I have a rails form which takes the rails model FlsCenter and orders them alphabetically to be displayed in a drop down. When none of the fields of the form are filled out, and the form is submitted, the page gets a 500 Server Error in which the following Rails error is displayed:
undefined method `map' for nil:NilClass
Where the map function is in the following view code:
= f.select(:fls_center, #fls_centers.map{|p| [p.name, p.id]}, prompt: "Select a Program Center", selected:#school_application.fls_center)
Where #fls_centers is defined in the new method of my SchoolApplicationController here:
#fls_centers = FlsCenter.order('name ASC')
Here are the relevant controller methods from SchoolApplicationsController
def create
#school_application = SchoolApplication.new(school_application_params)
if #school_application.save_with_payment
redirect_to pay_path(id: #school_application.id)
else
#school_application.errors.full_messages.each do |msg|
flash.now[:error] = msg
end
render action: "new"
end
end
And here is my new method:
def new
if application_params
#school_application = SchoolApplication.new(application_params)
else
#school_application = SchoolApplication.new()
end
Rails.logger.debug(#fls_centers)
#fls_centers = FlsCenter.order('name ASC')
end
The only thing I can imagine that is going wrong is that render action: "new" does not execute the variable instantiation inside the new method. I do not know how to amerliorate this situation. Thank you
You are executing Rails.logger.debug(#fls_centers) before defining the #fls_centers, So make changes like shown below:
def new
#fls_centers = FlsCenter.order('name ASC')
if application_params
#school_application = SchoolApplication.new(application_params)
else
#school_application = SchoolApplication.new()
end
Rails.logger.debug(#fls_centers)
end
Hope it helps!
You guessed right. The actual new method does not get executed when you call
render action: "new"
You will have to call the variable instantiations all over again:
def create
#school_application = SchoolApplication.new(school_application_params)
if #school_application.save_with_payment
redirect_to pay_path(id: #school_application.id)
else
#school_application.errors.full_messages.each do |msg|
flash.now[:error] = msg
end
#fls_centers = FlsCenter.order('name ASC')
render action: "new"
end
end
If you have a lot of them it might be better to refactor them in a method and call that method in the new method as well as before the render call.
I want users to be able to redeem their accounts, after they purchase them through a redemption code.
class Code < ActiveRecord::Base
#redemption_codes = Account.where(:redeemed == false).map(&:redemption_code)
validates :code, inclusion: { in: #redemption_codes }
end
Here's my create method in my AccountsController
def create
#code = Code.new(code_params)
#account = Account.where(redemption_code: #code)
if #code.save
#account.redeemed == true
flash[:notice] = "Account Redeemed"
redirect_to root_path
else
flash[:danger] = "This does not match any accounts"
render 'new'
end
end
After a code is saved, a user entering a redemption code, I want the :redeemed value of the item to change from false to true.
I got an error, I have yet to see before
Undefined Method 'redeemed' for #<ActiveRecord::Relation[]>
Not sure why though
It's because when you call Account.where(redemption_code: #code), that may return multiple accounts. You need to do something like Account.where(redemption_code: #code).first or Account.find_by_redemption_code(#code).
i try to test method create in my controller:
def create
#fund = Fund.new({started_at: Date.strptime(params[:fund].delete(:started_at), '%m/%d/%Y')}.merge(fund_params))
if #fund.save
flash[:alert] = "Fund #{#fund.name} saved!"
redirect_to funds_path
else
flash[:error] = "Fund #{#fund.name} could not be saved"
render :edit
end
end
file spec:
it 'Can create a new fund' do
fund = FactoryGirl.create(:fund)
post :create
expect(response).to redirect_to(funds_path)
end
And show this error:
NoMethodError: undefined method `delete' for nil:NilClass
The error is in this line, for the method 'delete':
#fund = Fund.new({started_at: Date.strptime(params[:fund].delete(:started_at),...
i don't know how solve this problem, Thanks.
As the error message says, that's because params[:fund] is nil. Try to send the proper parameters to the create action:
post :create, fund: { add_here_needed_params_for_this_action }
Example:
post :create, fund: { started_at: Date.today }
I have the following code, basically i want to save a cookie the first time a person submits the form, and the next time read from the cookie to populate the form.
def new
#detail = Detail.new(cookies[:detail])
end
And the create method has
def create
#detail = Detail.new(params[:detail])
if #detail.save
cookies[:detail] = {
value: params[:detail],
expires: 30.days.from_now
}
redirect_to confirmation_path
else
render 'new'
end
end
I get the error:
undefined method `reject' for #<String:0x007fbf5486ac48>
This is the error:
NoMethodError in VideosController#update
undefined method `each' for #<Topic:0x1032ee330>
This is the application trace:
app/models/video.rb:19:in `assign_topics'
app/controllers/videos_controller.rb:41:in `update'
app/controllers/videos_controller.rb:40:in `update'
this is my assign_topics method:
def assign_topics
if #topic_names
self.topics = Topic.find_or_create_by_name(#topic_names)
end
end
Note that I'm following this: http://media.railscasts.com/videos/167_more_on_virtual_attributes.mov
Here's the video controller's update method:
def update
#video = current_user.videos.find(params[:id])
respond_to do |format|
if #video.update_attributes(params[:video])
format.html { redirect_to(#video, :notice => 'Video was successfully updated.') }
else
format.html { render :action => "edit" }
end
end
end
I'd guess that your assign_topics method is at fault. Topic.find_or_create_by_name will return a single Topic instance, then you assign that to self.topics and self.topics is probably expecting an Array (or some other Enumerble); then later, the update process will try to loop through self.topics using each and you get your error.
You mention, in a comment, that you tried something like this:
self.topics = #topic_names.each { |n| Topic.find_or_create_by_name(n) }
But that won't work because each returns the original array and so the above is equivalent to this:
#topic_names.each { |n| Topic.find_or_create_by_name(n) }
self.topics = #topic_names
and all the Topic instances that you found/created are simply thrown away.
So, you might have better luck using collect like this:
def assign_topics
if #topic_names
self.topics = #topic_names.collect { |n| Topic.find_or_create_by_name(n) }
end
end
You are getting a NoMethodError Exception because somewhere in your code you are trying to loop, via .each(), on something that is not an array/enumerable.
According to your exception, you are calling .each() on a Model Object(Topic), which would make sense to not have the .each() method.