I had a lot of messy controller code that i''m in the process of tidying
up (moving logic into the models for example). Now, this no longer
works and i don''t understand the message "undefined method
`article=''
for #<Article:0x4518714>" - can anyone explain this to me please?
Here''s my stack trace, i''ll go through it step by step
afterwards -
C:/code/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1857:in
`method_missing''
C:/code/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1672:in
`send''
C:/code/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1672:in
`attributes=''
C:/code/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1671:in
`each''
C:/code/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1671:in
`attributes=''
C:/code/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1505:in
`initialize_without_callbacks''
C:/code/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/callbacks.rb:225:in
`initialize''
#{RAILS_ROOT}/app/models/article.rb:29:in `initialize''
#{RAILS_ROOT}/app/models/article.rb:48:in `new''
#{RAILS_ROOT}/app/models/article.rb:48:in `add_article''
#{RAILS_ROOT}/app/controllers/article_controller.rb:35:in `add''
OK - we start off with a form for getting some details for the new
object, an Article:
<% form_tag :controller => ''article'', :action =>
''add'' do %>
<p><label for="article_url">Url</label><br/>
<%= text_field ''article'', ''url''
%></p>
<p><label for="article_title">Title</label><br
/>
<%= text_field ''article'', ''title''
%><span></p>
<p><label for="article_tags">Tags</label><br/>
<%= text_field ''article'', ''tags''
%></p>
<%= submit_tag "Submit" %>
<% end %>
The results from this get passed through in a hash called :article to my
controller ''add'' method, which passes the params through the
the model''s
add_article method, along with the current user:
def add
if Article.add_article :article => params[:article], :user_id =>
session[:user]
flash[:notice] = "Added a new article."
else
flash[:notice] = "This article exists already. It has been
automatically scored up for you."
end
redirect_to :controller => ''article'', :action =>
''list''
end
In the model''s add_article method, i see if an article with the same
url
is already in the DB, and if it isn''t i call the constructor, just
passing through the same params, unchanged:
def Article.add_article(params = {})
#see if we''ve already got it - if not, make a new one, if we do,
score it up
article = Article.find(:first, :conditions => ["url = ?",
params[:article][:url]])
if article == nil
Article.new params
return true
else
article.change_score :user_id => params[:user_id], :points => 1
return false
end
end
And this is the constructor, which takes the params, which include the
:article hash and the :user_id value. I call super before anything else
to make sure i get an ActiveRecord object. (the hpricot code is just
used to autogenerate a title from a url)
def initialize(params = {})
super
if self.title == "" && self.url != ""
self.title = (Hpricot(open(self.url))/"title").first.inner_html
end
self.added_at = DateTime.now.to_s
self.points = 0
self.change_score :user_id => self.user_id, :points => 1
self.user_id = params[:user_id]
end
Looking at the trace, it looks like the call to super is causing the
problem (line 29 is the super call line) - but this is unchanged from
how it was working before. Can anyone see my problem?
thanks!
max
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---