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

This is the form:

<h3>Add post</h3>

<%= form_tag :controller=>'posts', :action=>'create' do %>   <%= label :q, :Title %>   <%= text_field :data, :title, :class => :addtextsize %><br/>   <%= label :q, :Content %>   <%= text_area :data, :content, :rows=>10 , :class => :addtextarea %><br/>   <%= label :q, :Tags %>   <%= text_field :data, :tags, :class => :addtextsize %><br/>   <%= 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:in `writer' 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:in `block 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:in `assign_attributes' activerecord (3.1.3) lib/active_record/base.rb:1567:in `initialize' app/controllers/posts_controller.rb:19:in `new' app/controllers/posts_controller.rb:19:in `create' actionpack (3.1.3) lib/action_controller/metal/implicit_render.rb:4:in `send_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:in `process_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:in `run_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:in `process_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:in `block 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:in `instrument' 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:in `process_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:in `process' actionpack (3.1.3) lib/abstract_controller/rendering.rb:45:in `process' actionpack (3.1.3) lib/action_controller/metal.rb:193:in `dispatch' actionpack (3.1.3) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch' actionpack (3.1.3) lib/action_controller/metal.rb:236:in `block 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:in `dispatch' 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:in `block 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:in `optimized_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:in `call' actionpack (3.1.3) lib/action_dispatch/routing/route_set.rb:532:in `call' warden (1.1.0) lib/warden/manager.rb:35:in `block in call' warden (1.1.0) lib/warden/manager.rb:34:in `catch' warden (1.1.0) lib/warden/manager.rb:34:in `call' actionpack (3.1.3) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call' rack (1.3.5) lib/rack/etag.rb:23:in `call' rack (1.3.5) lib/rack/conditionalget.rb:35:in `call' actionpack (3.1.3) lib/action_dispatch/middleware/head.rb:14:in `call' 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:in `call' rack (1.3.5) lib/rack/session/abstract/id.rb:195:in `context' rack (1.3.5) lib/rack/session/abstract/id.rb:190:in `call' actionpack (3.1.3) lib/action_dispatch/middleware/cookies.rb:331:in `call' activerecord (3.1.3) lib/active_record/query_cache.rb:64:in `call' 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:in `block 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:in `run_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:in `call' rack (1.3.5) lib/rack/sendfile.rb:101:in `call' actionpack (3.1.3) lib/action_dispatch/middleware/remote_ip.rb:48:in `call' actionpack (3.1.3) lib/action_dispatch/middleware/show_exceptions.rb:47:in `call' railties (3.1.3) lib/rails/rack/logger.rb:13:in `call' rack (1.3.5) lib/rack/methodoverride.rb:24:in `call' rack (1.3.5) lib/rack/runtime.rb:17:in `call' activesupport (3.1.3) lib/active_support/cache/strategy/local_cache.rb:72:in `call' rack (1.3.5) lib/rack/lock.rb:15:in `call' actionpack (3.1.3) lib/action_dispatch/middleware/static.rb:53:in `call' railties (3.1.3) lib/rails/engine.rb:456:in `call' rack (1.3.5) lib/rack/content_length.rb:14:in `call' railties (3.1.3) lib/rails/rack/log_tailer.rb:14:in `call' 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:in `service' /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:in `block 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"}

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 Law 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