Getting error, 'NoMethodError in PostsController#create' , manytomany relation

models:

post.rb:
has_many :ptmaps
has_many :tags, :through => :ptmaps

tag.rb:
has_many :ptmaps
has_many :posts, :through => :ptmaps

ptmap.rb:
belongs_to :post
belongs_to :tag

This is the form:

Add post

<%= form_tag :controller=>‘posts’, :action=>‘create’ do %>
<%= label :q, :Title %>
<%= text_field :data, :title, :class => :addtextsize %>

<%= label :q, :Content %>
<%= text_area :data, :content, :rows=>10 , :class => :addtextarea
%>

<%= label :q, :Tags %>
<%= text_field :data, :tags, :class => :addtextsize %>

<%= label :q, :Submit %>
<%= submit_tag “Add Post” %>
<% end %>

I put “linux dev” in tags textbox.

This is the action:

def create
@addpost = Post.new params[:data]
@addpost.user_id = current_user.id
print @addpost
end

I got error for this line: @addpost = Post.new params[:data]

Error:

NoMethodError in PostsController#create

undefined method `each’ for “linux dev”:String

activerecord (3.1.3)
lib/active_record/associations/collection_association.rb:299:in
replace' activerecord (3.1.3) lib/active_record/associations/collection_association.rb:41:inwriter’
activerecord (3.1.3)
lib/active_record/associations/builder/association.rb:49:in block in define_writers' activerecord (3.1.3) lib/active_record/base.rb:1751:inblock in
assign_attributes’
activerecord (3.1.3) lib/active_record/base.rb:1747:in each' activerecord (3.1.3) lib/active_record/base.rb:1747:inassign_attributes’
activerecord (3.1.3) lib/active_record/base.rb:1567:in initialize' app/controllers/posts_controller.rb:19:innew’
app/controllers/posts_controller.rb:19:in create' actionpack (3.1.3) lib/action_controller/metal/implicit_render.rb:4:insend_action’
actionpack (3.1.3) lib/abstract_controller/base.rb:167:in
process_action' actionpack (3.1.3) lib/action_controller/metal/rendering.rb:10:inprocess_action’
actionpack (3.1.3) lib/abstract_controller/callbacks.rb:18:in block in process_action' activesupport (3.1.3) lib/active_support/callbacks.rb:425:in_run__426936025__process_action__703550844__callbacks’
activesupport (3.1.3) lib/active_support/callbacks.rb:386:in
_run_process_action_callbacks' activesupport (3.1.3) lib/active_support/callbacks.rb:81:inrun_callbacks’
actionpack (3.1.3) lib/abstract_controller/callbacks.rb:17:in
process_action' actionpack (3.1.3) lib/action_controller/metal/rescue.rb:17:inprocess_action’
actionpack (3.1.3) lib/action_controller/metal/instrumentation.rb:30:in
block in process_action' activesupport (3.1.3) lib/active_support/notifications.rb:53:inblock
in instrument’
activesupport (3.1.3)
lib/active_support/notifications/instrumenter.rb:21:in instrument' activesupport (3.1.3) lib/active_support/notifications.rb:53:ininstrument’
actionpack (3.1.3) lib/action_controller/metal/instrumentation.rb:29:in
process_action' actionpack (3.1.3) lib/action_controller/metal/params_wrapper.rb:201:inprocess_action’
activerecord (3.1.3)
lib/active_record/railties/controller_runtime.rb:18:in process_action' actionpack (3.1.3) lib/abstract_controller/base.rb:121:inprocess’
actionpack (3.1.3) lib/abstract_controller/rendering.rb:45:in process' actionpack (3.1.3) lib/action_controller/metal.rb:193:indispatch’
actionpack (3.1.3) lib/action_controller/metal/rack_delegation.rb:14:in
dispatch' actionpack (3.1.3) lib/action_controller/metal.rb:236:inblock in
action’
actionpack (3.1.3) lib/action_dispatch/routing/route_set.rb:65:in call' actionpack (3.1.3) lib/action_dispatch/routing/route_set.rb:65:indispatch’
actionpack (3.1.3) lib/action_dispatch/routing/route_set.rb:29:in call' rack-mount (0.8.3) lib/rack/mount/route_set.rb:152:inblock in call’
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:96:in block in recognize' rack-mount (0.8.3) lib/rack/mount/code_generation.rb:75:inoptimized_each’
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:95:in recognize' rack-mount (0.8.3) lib/rack/mount/route_set.rb:141:incall’
actionpack (3.1.3) lib/action_dispatch/routing/route_set.rb:532:in
call' warden (1.1.0) lib/warden/manager.rb:35:inblock in call’
warden (1.1.0) lib/warden/manager.rb:34:in catch' warden (1.1.0) lib/warden/manager.rb:34:incall’
actionpack (3.1.3)
lib/action_dispatch/middleware/best_standards_support.rb:17:in call' rack (1.3.5) lib/rack/etag.rb:23:incall’
rack (1.3.5) lib/rack/conditionalget.rb:35:in call' actionpack (3.1.3) lib/action_dispatch/middleware/head.rb:14:incall’
actionpack (3.1.3) lib/action_dispatch/middleware/params_parser.rb:21:in
call' actionpack (3.1.3) lib/action_dispatch/middleware/flash.rb:247:incall’
rack (1.3.5) lib/rack/session/abstract/id.rb:195:in context' rack (1.3.5) lib/rack/session/abstract/id.rb:190:incall’
actionpack (3.1.3) lib/action_dispatch/middleware/cookies.rb:331:in
call' activerecord (3.1.3) lib/active_record/query_cache.rb:64:incall’
activerecord (3.1.3)
lib/active_record/connection_adapters/abstract/connection_pool.rb:477:in
call' actionpack (3.1.3) lib/action_dispatch/middleware/callbacks.rb:29:inblock in call’
activesupport (3.1.3) lib/active_support/callbacks.rb:392:in
_run_call_callbacks' activesupport (3.1.3) lib/active_support/callbacks.rb:81:inrun_callbacks’
actionpack (3.1.3) lib/action_dispatch/middleware/callbacks.rb:28:in
call' actionpack (3.1.3) lib/action_dispatch/middleware/reloader.rb:68:incall’
rack (1.3.5) lib/rack/sendfile.rb:101:in call' actionpack (3.1.3) lib/action_dispatch/middleware/remote_ip.rb:48:incall’
actionpack (3.1.3)
lib/action_dispatch/middleware/show_exceptions.rb:47:in call' railties (3.1.3) lib/rails/rack/logger.rb:13:incall’
rack (1.3.5) lib/rack/methodoverride.rb:24:in call' rack (1.3.5) lib/rack/runtime.rb:17:incall’
activesupport (3.1.3)
lib/active_support/cache/strategy/local_cache.rb:72:in call' rack (1.3.5) lib/rack/lock.rb:15:incall’
actionpack (3.1.3) lib/action_dispatch/middleware/static.rb:53:in call' railties (3.1.3) lib/rails/engine.rb:456:incall’
rack (1.3.5) lib/rack/content_length.rb:14:in call' railties (3.1.3) lib/rails/rack/log_tailer.rb:14:incall’
rack (1.3.5) lib/rack/handler/webrick.rb:59:in service' /home/user1/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/httpserver.rb:111:inservice’
/home/user1/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/httpserver.rb:70:in
run' /home/user1/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/server.rb:183:inblock in start_thread’

Why am i getting this error?

This is an issue at the model / db layer. To help, we’d need the model
code
and the contents of params[:data]

John H. wrote in post #1048579:

This is an issue at the model / db layer. To help, we’d need the model
code
and the contents of params[:data]

I have added the model relation, and this is the params[:data]

“data”=>{“title”=>“Linux Developers In High Demand: Report”,
“content”=>“The Linux Foundation and Dice have released a report titled
‘Linux Jobs Report’ which shows the increasing demand for Linux talent
across industries. The report includes responses from more than 2,000
hiring managers at corporations, Small and Medium Businesses (SMBs),
government organizations, and staffing agencies from across the globe.”,
“tags”=>“linux dev”}, “commit”=>“Add Post”}

On 24 February 2012 04:10, gmspro gmspro [email protected] wrote:

across industries. The report includes responses from more than 2,000
hiring managers at corporations, Small and Medium Businesses (SMBs),
government organizations, and staffing agencies from across the globe.",
“tags”=>“linux dev”}, “commit”=>“Add Post”}

I can’t see where you have added the model code. Do you have post
has_many tags, or has_and_belongs_to_many tags? If so then the
problem may be that you are passing a string for tags instead of an
array. Just a guess as you have not shown us the model relationships.

Colin

Colin L. wrote in post #1048610:

I can’t see where you have added the model code. Do you have post
has_many tags, or has_and_belongs_to_many tags? If so then the
problem may be that you are passing a string for tags instead of an
array. Just a guess as you have not shown us the model relationships.

Colin
See the original post.

On 27 February 2012 00:46, gmspro gmspro [email protected] wrote:

Colin L. wrote in post #1048610:

I can’t see where you have added the model code. Do you have post
has_many tags, or has_and_belongs_to_many tags? If so then the
problem may be that you are passing a string for tags instead of an
array. Just a guess as you have not shown us the model relationships.

Colin
See the original post.

No, still can’t see it, maybe I am missing the original post, post it
again please.
Why did you not comment on my suggestions above?

Colin

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs