简体   繁体   中英

Issue with railscast# 238 , date_select not working with rails 4.

I am trying railscast#238. I am using rails 4 and mongoid(4.0.0).When trying to add published_on field with date_select helper. It is not able to save into collection. Then, I added :published_on in the permit function. But then also I am getting error. I also tried including Mongoid::MultiParameterAttributes but it is not found. I think its been removed from mongoid. Below is the error in the log file:

Log File

Processing by ArticlesController#update as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"3+y5UUckEDqJRPwdibdlhwAxKHi1g2ECF/4SzFNVbJE=", "article"=>{"name"=>"sachin", "published_on(1i)"=>"2013", "published_on(2i)"=>"4", "published_on(3i)"=>"10", "content"=>"s'up?"}, "commit"=>"Update Article", "id"=>"522f4ce6f0b1817131000001"} MOPED: 127.0.0.1:27017 QUERY database=mongs_development collection=articles selector={"_id"=>"522f4ce6f0b1817131000001"} flags=[:slave_ok] limit=0 skip=0 batch_size=nil fields=nil (0.5178ms) Completed 500 Internal Server Error in 3ms

Mongoid::Errors::UnknownAttribute ( Problem: Attempted to set a value for 'published_on(1i)' which is not allowed on the model Article. Summary: Without including Mongoid::Attributes::Dynamic in your model and the attribute does not already exist in the attributes hash, attempting to call Article#published_on(1i)= for it is not allowed. This is also triggered by passing the attribute to any method that accepts an attributes hash, and is raised instead of getting a NoMethodError. Resolution: You can include Mongoid::Attributes::Dynamic if you expect to be writing values for undefined fields often.): app/controllers/articles_controller.rb:44:in block in update' app/controllers/articles_controller.rb:43:in update'

If i remove published_on from permit:

arameters: {"utf8"=>"✓", "authenticity_token"=>"3+y5UUckEDqJRPwdibdlhwAxKHi1g2ECF/4SzFNVbJE=", "article"=>{"name"=>"sachin", "published_on(1i)"=>"2013", "published_on(2i)"=>"9", "published_on(3i)"=>"10", "content"=>"s'up?"}, "commit"=>"Update Article", "id"=>"522f4ce6f0b1817131000001"} MOPED: 127.0.0.1:27017 COMMAND database=admin command={:ismaster=>1} (0.6187ms) MOPED: 127.0.0.1:27017 QUERY database=mongs_development collection=articles selector={"_id"=>"522f4ce6f0b1817131000001"} flags=[:slave_ok] limit=0 skip=0 batch_size=nil fields=nil (0.2012ms) Unpermitted parameters: published_on(1i), published_on(2i), published_on(3i) Redirected to

       http://localhost:3000/articles/522f4ce6f0b1817131000001

Completed 302 Found in 5ms

Started GET "/articles/522f4ce6f0b1817131000001" for 127.0.0.1 at 2013-09-10 23:19:34 +0530 Processing by ArticlesController#show as HTML Parameters: {"id"=>"522f4ce6f0b1817131000001"} MOPED: 127.0.0.1:27017 QUERY database=mongs_development collection=articles selector={"_id"=>"522f4ce6f0b1817131000001"} flags=[:slave_ok] limit=0 skip=0 batch_size=nil fields=nil (0.3569ms) Rendered articles/show.html.erb within layouts/application (0.6ms) Completed 200 OK in 9ms (Views: 7.4ms)

Can someone give solution for this?

You need to include Mongoid::MultiParameterAttributes in your document class.

In Mongoid 4.0 you also need to define that module in yor lib/ folder. See detailed explanation in this blog

I would bet that you have the function

def article_params params.require(:article).permit!(:published_on) end

But you also need...

Article.create(article_params)

NOT

Article.create(params[:article])

Hope this helps,

-Brian

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM