Rails Guides - Step 11 - Multi Model Form - MassAssignmentSecurity error - ruby-on-rails

I'm very carefully following the RailsGuide and it's working beautifully, until Step 11.
Adding Tags via a MultiModel form.
Once I've added the code I get this error:
ActiveModel::MassAssignmentSecurity::Error in PostsController#update
Can't mass-assign protected attributes: tags_attributes
Application Trace:
app/controllers/posts_controller.rb:65:in block in update'
app/controllers/posts_controller.rb:64:inupdate'
I've backed up and started over a few times to be certain it's not user error.
Can someone help me get past this step. I'm so close to done ! Then I can move on to Hartl's tutorial.
Thank you.

This is not your fault!
The deployed Getting Started guide hasn't been updated for the Rails 3.2.3 default of
# in config/application.rb:
config.active_record.whitelist_attributes = true
The fix is to add this to your Post model:
# in app/models/post.rb
class Post < ActiveRecord::Base
attr_accessible :tags_attributes
...
Incidentally, this whole section was removed from the source of the guide going forward, which you can find at https://github.com/rails/rails/blob/master/guides/source/getting_started.textile .

Related

Validation not displaying error messages Ruby on Rails

I'm new to Ruby Rails and trying to follow along in the textbook, but my validation code isn't working. The original project was created by using the scaffold command:
rails generate scaffold Micropost content:text user_id:integer
Then the instructions tell me to add the following code to my Micropost model:
validates :content, length: { maximum: 140 }, presence: true
The validation works in that the form is not submitted if the length is longer than 140, but the problem is that there is no error message or anything. The button just does nothing. There is supposed to be a red error message, according to the textbook.
Any ideas?
It seems that your form does not show validation errors.
Check out this answer https://stackoverflow.com/a/7880392/4077816.
So I actually just discovered the problem a couple days ago. I was looking into this again for a Rails project I'm been working on, and discovered that in Rails 7 the Turbo Gem that is now included by default will only process render statements on the same controller if we add unprocessable_entity as the status.
So the validation was working, but the user couldn't see anything because the rendering was not working.

Rails 6 app not validating belongs_to associations

I have a rails engine which encapsulates a piece of my application's funtionality. I have a bunch of models in the engine, which have various belongs_to associations defined. As of rails 5 these associations are supposed to be required by default, unless optional: true is specified in the definition.
I’m still able to create instances of the models without any validation errors. I haven’t specified optional: true on any of the associations, nor is the config optionconfig.active_record.belongs_to_required_by_default set anywhere. Besides, it was removed in rails 6 anyway.
I can't think of any reason the model instances would not fail validation. I would expect any instances of any model with an undefined belongs_to association would be invalid and raise an error. Why would these records pass validation?
I found my problem, thanks to #MatthiasWinkelmann for the tip. It turns out my engine was not calling load_defaults at all. I needed to add the following to spec/dummy/config/application.rb:
module Dummy
class Application < Rails::Application
config.load_defaults Rails::VERSION::STRING.to_f
... etc ....
end
end
here is an article containing more explanation:
An upgraded Rails gem does not upgrade your Rails configuration
I probably would have done better to mention in my question that I'm in the process of upgrading my application from Rails 4.2 to 6.1. The change was introduced in Rails 5.

ruby on rails // action cable chat tutorial error

I followed the https://www.sitepoint.com/rails-and-actioncable-adding-advanced-features tutorial, but it says:
NameError in PersonalMessagesController#create uninitialized constant
PersonalMessage::NotificationBroadcastJob
after_create_commit do
conversation.touch
NotificationBroadcastJob.perform_later(self)
end
end
The above error appears when I submit, but when I go back I get a message.
So I deleted code:
after_create_commit do
conversation.touch
NotificationBroadcastJob.perform_later(self)
end
end
and tried to run it. I did not see the same error, but this time I can not chat live.
I followed the tutorial faithfully. I do not know why two problems arise. I need help.
my github : https://github.com/sangyeol-kim/stackoverflow_ask_ac2
Try fixing file name notifications_broadcast_job.rb -> notification_broadcast_job.rb

Conflict between Rails Admin and Impressionist gems

I'm using the latest version of the Impressionist and Rails Admin gems, and wondering if anyone could shed some light on an annoying conflict I'm experiencing. The problem is roughly documented here - https://github.com/sferik/rails_admin/issues/1315, yet the vaguely described solution is not working for me. When I have the line is_impressionable in my Listing model, I get an error when starting my Rails server with rails s:
...rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.2/lib/active_record/dynamic_matchers.rb:22:in `method_missing': undefined local variable or method `is_impressionable' for Listing(no database connection):Class (NameError)
If I first start the server, and then add the 'is_impressionable' line, everything works fine, so the problem only occurs during initialization. I don't fully understand the initialization process, so am not sure how to go about getting this to work.
I have tried moving all my rails_admin model configuration options to their respective models, rather than in the initializer, which had no effect. I also have the following line in my initializer:
config.included_models = [Listing,ListingImage,AllOtherModelsHere...]
I have tried adding single quotes around these model names, which results in the following errors, as described in the github issue here
[RailsAdmin] Could not load model Listing, assuming model is non existing. (undefined local variable or method `is_impressionable' for Listing(no database connection):Class)
Any ideas what else I can try to make these gems work together? I don't want to have to remove the is_impressionable line every time I want to restart the server or generate a migration...
Not sure if the same issue that I had but yet I will post what worked for me just in case someone struggles with this too:
Im on a ruby 2.1.5 project with rails 4.2.0 and among other gems I'm using rails admin.
I run into several weird problems trying to set this up. For instance if I added the is_impressionable call within one of my models for some reason the execution of that file stopped there and I started getting weird errors like any method declared below the is_impressionable failed with undefined error.
So what I end up doing was:
class MyModel < ActiveRecord::Base
include Impressionist::IsImpressionable
is_impressionable
end
So this solved my issue and now i can access #my_model_instance.impression_count as expected.
I changed every occurrence of Klass to 'Klass'.constantize in initializer.

rails resque and resque-send-later plugins causing "unitialized constant ClassName::Resque" error

i'm using the resque and resque-send-later PLUGINS (not gems) in my project.
I haven't put 'require' statements anywhere in the code at all (since they're plugins and so they must be included upon initialization).
the app is working perfectly locally, but on heroku, it shows an error
"const_missing: unitialized constant User::Resque"
my User model:
class User < ActiveRecord::Base
include Resque::Plugins::SendLater
def self.testingWorker1
# code to be run in the background
end
end
my User_controller: (where i'm calling the above method from)
class UserController < ApplicationController
def testingResqueWorker
User.send_later(:testingWorker1)
end
end
so I removed the line include Resque::Plugins::SendLater from the my model
it still works perfectly locally, but now on heroku it gives an error saying "method_missing: send_later"
my question is:
1. how do we 'include' or 'require' plugins in rails? are they automatically available to all controllers and models?
2. any ideas for how to fix the above errors?
Two thoughts
any reason why you aren't using the gems?
are you sure the plugins have been added to the git repository and therefore have been deployed to heroku?

Resources