I have a relationship user (has_many :reviews) and review ( belongs_to :user) and I'm getting a NoMethodError when I try to execute create in reviews_controller.
def create
#review = #user.reviews.create(:service_id => 10)
end
The same query works fine in the console but it gives that error when I do it through the controller.
What am I missing here?
Thank you.
Print out #user details to log to verify current_user is populating the value correctly.
Related
I am moving an application from Rails 2.2.2 to Rails 3. I have a form that is used to update a user's info that was fully functional in Rails 2 but breaks in Rails 3
When the form is submitted, a method is called that creates the user object and then tries to do the update like this:
if #user.update_attributes params[:user] ## line 126
Then the controller throws this exception:
undefined method `update_attributes' for #<ActiveRecord::Relation:0xacfc178>
in: app/controllers/admin_controller.rb:126:in `save_user'
So it looks like ActiveRecord in Rails 3 is returning a different type of object? One that doesn't inherit update_attributes? How do I fix this?
Here is the full controller method in question:
def save_user
#needs_password_gen = "YES"
#user = B2bUser.where("id = ?",params[:id])
#needsAPICredentials = false
##### Make sure thay gave us an email address
if !params[:user][:email] || !validEmailAddress(params[:user][:email].to_s)
flash[:warning] = "Valid email address is required."
redirect_to :controller => "admin/edit_user/#{#user.id}" and return
end
#user.first.update_attributes params[:user]
end
THANKS
Looks like your #user object may be an array. You are probably using :where to query and are forgetting to pop it off the array. Try this:
#user.first.update_attributes params[:user]
In products_controller.rb
# GET /search/'brand'
def brand
#product = Product.find_all_by_brand(params[:brand])
respond_to do |format|
format.html # brand.html.erb
end
end
In routes.rb
match '/search/:brand' => 'products#brand'
If I try accessing localhost:3000/search/Apple I get the following error Couldn't find Product with id=Apple
Is there anything that I'm missing? Are there any other files that I should handle?
Update
Now I'm getting undefined method 'size' for nil:NilClass and I'm not even sure what I changed.
The query executed by rails is select "products".* FROM "products" WHERE "products"."brand" = 'Apple' ORDER BY last_seen DESC and they seem to return the correct products.
Application Trace
app/views/products/_product.html.erb:1:in
_app_views_products__product_html_erb___2255278_29707176'
app/views/products/brand.html.erb:1:in
_app_views_products_brand_html_erb___464952485_38589588'
app/controllers/products_controller.rb:52:in `brand'
The comments in your controller indicate that you might want to try /search/brand/Apple.
Found the error, the erb page was receiving product instead of products. Typing error.
I'm following the Michael Hartl tutorial and I found a problem and don't know how solved it... my problem is around here whitout ajax.
when I try to do follow to other user I got this:
NoMethodError (undefined method `[]' for nil:NilClass):
app/controllers/follows_controller.rb:4:in `create'
here is my "follows" controller eq to relationships
before_filter :authenticate_user!
def create
#user = User.find(params[:follows][:followed_id])
current_user.follow!(#user)
redirect_to #user
end
def destroy
#user = Follow.find(params[:id]).followed
current_user.unfollow!(#user)
redirect_to #user
end
the rest (models,rspec) I have the same
if you need more code tell me, thanks
Chances are that params[:follows] is nil. Check what parameters you're submitting to the create action.
I am trying to replace user profile views of the sort
/users/1
with
/username
I'm aware this means I'll need to check for collisions of various kinds. I've consulted the following similar SO questions:
Ruby on rails routing matching username
customize rails url with username
Routing in Rails making the Username an URL:
routing error with :username in url
Here are the various failed routes.rb route definitions I've tried, and associated errors:
match "/:username" => "users#show", via: "get"
Here's the error:
ActiveRecord::RecordNotFound in UsersController#show
Couldn't find User without an ID
app/controllers/users_controller.rb:7:in `show'
Here is my corresponding users_controller:
6 def show
7 #user = User.find(params[:id])
8 end
match "/:username" => 'users#show', :as => :profile
Same error as above.
match "/:username", :controller => "users/:id", :action => 'show'
Routing Error
uninitialized constant Users
Try running rake routes for more information on available routes.
match '/:username', :controller => 'users', :action => 'show'
Same error as 1.
match '/:username', to: 'users/:id', via: 'show'
Server does not start.
match "/:username" => redirect("/users/:id")
Error:
ActiveRecord::RecordNotFound in UsersController#show
Couldn't find User with id=:id
Any idea why my routing is not working the same way that everyone else who asks this question's is?
Update
Just to take this issue out of the comments and put it in the question more cleanly. After making the change by #Ryan Bigg below, I had a routing problem in my redirect to profile when a new one is created. Here's my create code:
def create
#user = User.new(params[:user])
if #user.save
session[:user_id] = #user.id
flash[:success] = "Thank you for signing up."
redirect_to ('/'+#user.username)
#redirect_to #user, notice: "Thank you for signing up!"
else
render "new"
end
end
And here is my user.rb
def to_param
self.username
#username
end
However, the commented out redirect, which I think should work with the to_param update, doesn't work, while the ugly hackish one above it does. Why is the to_param overwrite, which worked for other people, not working on my app? My #update and #edit methods are also not working, as their redirects go to "users/1/edit" instead of "username/edit" if overwriting to_param doesn't take care of this.
The first one is correct, but isn't working because you're still attempting to do something like this inside your controller:
User.find(params[:username])
When you should instead be doing this:
User.find_by_username!(params[:username])
The first one will attempt to find by the primary key of your table, where the second one will, correctly, query on the username field instead.
In addition to the update for to_params, the bottom of the routes file needs the following line:
resources :users, :path => '/'
I'm trying to use activeadmin's batch_action so I can run actions on more than one record. However when trying to run my rails server I get the following error.
undefined method `batch_action' for #<ActiveAdmin::ResourceDSL:0xb11f980> (NoMethodError)
Here is the activeadmin resource code:
ActiveAdmin.register Product do
batch_action :destroy, false
filter :name
index do
selectable_column
column :name
default_actions
end
controller do
def current_company
#current_company
end
end
end
I'm not sure where I'm getting it wrong - I need to show a corresponding checkboxes against the records and then define a batch action. Where am I getting it wrong here?
Got the answer :) was a wrong entry in my gemfile.
https://github.com/gregbell/active_admin/issues/1302