If the dispute save the change the order boolean false to true
but after create the rails log show
undefined method `update_attributes' for false:FalseClass
Someone know why?
order
has_one :dispute
dispute
belongs_to :order
def create
if current_user == #order.buyer
dispute = #order.dispute.nil? ? Dispute.new : #order.dispute
if dispute.save
#order = params[:dispute_status] == "1"
#order.update_attributes(:dispute_status => true)
redirect_to order_dispute_path(#order, #dispute)
flash[:success] = 'yess'
else
flash[:error] = 'Erro'
redirect_to :back
end
end
end
And if i remove the #order.update_attributes(:dispute_status => true)
the logs shows :
ActionController::RoutingError (No route matches {:action=>"create",
:controller=>"disputes", :order_id=>false, :format=>nil}
Well you have #order = params[:dispute_status] == 1. This returns a boolean. In your case, probably false. So now #order is a boolean false and you are trying to call update_attributes on it.
FalseClass.instance_methods.include?(:update_attributes) # false
ActiveRecord::Base.instance_methods.include?(:update_attributes) # true
your #order isn't a ActiveRecord::Base instance! clear?
Related
Rails 5.2
In my inventories_controller.rb, I have the following:
before_action :fetch_product, only: [:show]
def show
........
end
def fetch_product
if params.has_key?(:sku)
#product = Product.get_product(params)
end
end
This works fine, when I do: http://0.0.0.0:3000/sku/12345678
I am trying to implement search functionality, so I modified nventories_controller.rb as follows:
def fetch_product
if params.has_key?(:search) && !params[:search].blank?
product = Product.find_by_sku(params[:search])
if !product
params = params.except[:search]
redirect_to product_show_path, alert: 'Product was not found'
end
params = params.merge!(:sku, product.sku)
end
if params.has_key?(:sku)
#product = Product.get_product(params)
end
end
When I do: http://0.0.0.0:3000/sku/12345678
I get an instant error message:
undefined method `has_key?' for nil:NilClass
Using my debugger, I find that on entry into the fetch_product method, params is nil
Any idea what's going on?
params = params.merge!(:sku, product.sku) modifies the hash in place and returns nil, don't do that assignment, just call params.merge! (if you still want to do the assignment, remove the "!").
Personally, I wouldn't modify the params hash unless it's really really needed, I would use another variable.
I have in my orders/edit.html.erb a form that begins like this:
<%= simple_form_for [#reservation, #order] do |f| %>
Orders Controller:
def edit
#reservation = Reservation.find_by_id(params[:reservation_id])
#order = Order.find_by_id(params[:id])
end
Association:
Reservation :has_one Order
Routes:
resources :reservations do
resources :orders
end
If my path resembles something like /reservations/10/orders/10/edit I end up getting an error from Rails saying NoMethodError in Orders#edit and undefined method 'model_name' for nil:NilClass
When I create a new order the form works perfectly fine so not sure why i'm getting an error all of a sudden, can someone help me with this issue?
Current implementation is prone to failure when the URL supplied either a reservation ID or an order ID that is not valid. Two ways to handle this:
First, let Rails do it for you:
def edit
#reservation = Reservation.find(params[:reservation_id])
#order = Order.find(params[:id])
end
This will raise an ActiveRecord::RecordNotFound error, which, in production, should lead users to your 404 page.
If you'd prefer to keep find_by_id and handle this manually, or to rescue from this error in a different way, you can do:
def edit
#reservation = Reservation.find_by_id(params[:reservation_id])
#order = Order.find_by_id(params[:id])
if #reservation.nil? || #order.nil?
raise ActiveRecord::RecordNotFound.new # Or, do something else
end
end
That would yield the same result as above ... just more code.
I have such a controller action:
def update_grade
#id = params[:id]
#grade = "Grade#{#id}".classify.constantize.find_by_id(params[:grade_id])
# render :text => #grade.grade1.inspect
respond_to do |format|
if #grade.update(:grade1 => 2)
format.html { redirect_to update_grade_homework_path, notice: 'Homework was successfully updated.' }
end
end
end
I am getting "Undefined method `update'" error but #grade is perfectly fine object. I verified it by inspecting it. What am I doing wrong?
ActiveRecord::Base doesn't have an instance method called update. (Well, actually, it does, but it's a private method so you shouldn't be able to call it using #grade.update). You may be looking for update_attributes.
#grade.update_attributes(:grade1 => 2)
(Note that if you're using Rails 3 you'll need to add the line attr_accessible :grade1 to your model or the above code will throw a MassAssignmentSecurityError.)
ActiveRecord does have a class method called update, which you can find in the docs here.
The difference in usage should be clear from this example (say you have a model Grade and you want to set the attribute "foo" to "bar" for the Grade with id 5):
# Using the class method "update":
Grade.update(5, foo: "bar")
# Using the instance method "update_attributes"
#grade = Grade.find_by(id: 5) # Or just Grade.find(5)
#grade.update_attributes(foo: "bar")
That being said, if the error message really does say "Undefined method `update' for nil:NilClass" and #grade really doesn't equal nil, then that is a bizarre error and I can't think of any reason why it might be occurring. Try changing update to update_attributes and let me know if that works.
def has_photo
if user_signed_in?
#user = User.where(:id => current_user.id).first
if #user.has_photo?
if Asset.where(:attachable_id => current_user.id).count < 4
def sub_layout
"application"
end
render :template => "profiles/no_photo"
end
end
end
end
What would be the correct way to compare the Asset.count ?
Asset.where is a query, you would be much better using relationships for this.
If
Class User
has_many :assets
end
Class Asset
belongs_to :user
end
You could just use:
#user.assets.count < 4
As long as Asset has a user_id field (or make the relationship use :attachable_id) that is correctly set (the relationship can do that too if you create the Asset correctly)
By the way, if :id is unique for each user (it should be) you can rewrite
#user = User.where(:id => current_user.id).first
as
#user = User.find(current_user.id)
Hope it helps
Ok, im new to ruby and rails, I cant seem to get which I would assume to be simple working. Basically my code is to remove a reference to an association (nil it).
I want to get the name member (Team.name) of the model and save it to (team_name) before removing the association for the flash message.
Here is the controller action and Models:
def remove_resource
...
#user = User.find(params[:user_id])
team_name = #user.team.name
#user.team = nil
if #user.save
flash[:notice] = "#{#user.name} was removed from "+team_name+". They are no longer on a team."
...
end
...
end
class Team < ActiveRecord::Base
has_many :user, :class_name => "User", :foreign_key => "user_id",
:order => "team_start"
...
def ==(another_team)
if self.name != another_team.name then
return false
end
if self.location != another_team.location then
return false
end
...
true
end
...
end
class User < ActiveRecord::Base
belongs_to :team, :class_name => "Team",
:foreign_key => "team_id"
...
end
The Errors I am receiving:
NoMethodError in UsersController#remove_resource
undefined method `name' for nil:NilClass
The trace:
app/models/user.rb:50:in `=='
app/controllers/teams_controller.rb:50:in `remove_resource'
Now the error only occurs when I retrieve the
#user.team.name
If I delete this and the reference to the variable team_name, all works fine.
Thanks!
You get this error because you dont have the #user.team associated anymore. There is only a nil which doesnt have the Model specific method name. So you need to validate if threre is a team associated or not before executing methods of a associated object that may not be there.
def remove_resource
...
#user = User.find(params[:user_id])
if #user.team.nil? # Check if theres a associated object
#do sth. when theres no team to remove
else
team_name = #user.team.name
#user.team = nil
if #user.save
flash[:notice] = "#{#user.name} was removed from "+team_name+". They are no longer on a team."
...
end
...
end