Reputation: 267320
I am doing this:
user = User.find( params[:id] )
when I do:
user.section_id
it says:
nomethoderror exception undefined method 'section_id' for <#Array:0x.....>
Why does it say <#Array?
In rails console, everything works fine when I do: u = User.find(2)
Update
My javascript is doing this:
$.ajax({
type: "POST",
url: "/user/add_section",
dataType: "html",
data: { id: id, section_text: sectionText
},
success: function(payload) {
},
error: function(jqx, ts, et) {
},
complete: function(){}
});
Using chrome I can see that the id is 8.
My controller action:
def add_section
user = User.find(params[:id])
section_id = user.section_id # error!!
end
I put a debugger in this code, and the object does load properly, it just that it loads into an array so this works:
user[0].section_id
but in irb it works fine with: user.section_id for the exact same value passed to the find method.
Upvotes: 1
Views: 700
Reputation: 83680
user = User.find( params[:id] )
Looks like params[:id]
return an Array, so user
- is an Array of users actually. Or it can be just one id wrap into []
like: user = User.find [8]
- which will still return an Array with one element
So you should debug your params[:id]
.
UPD
Also here is a problem in your application. It is not REST. You should call /user/:id/add_section
path instead of /user/add_section
.
Your routes:
resources :users do
post :add_section, :on => :member
end
Upvotes: 1