Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialpatte bläsing
331 PointsRails 4 and Treebook - Unpermitted parameter: attachment
I did everything as said in the videos but I cant get the file attachment to work with rails 4....
the form works as expected:
Parameters: {"utf8"=>"?", "authenticity_token"=>"Bkz9fQ3WjvLXhzvRnCfmlVGijXaVVLYjsqDLVUguZ0M=", "status"=>{"content"=>"test test", "document_attributes"=>{"attachment"=>#<ActionDispatch::Http::UploadedFile:0x007f93b58500c0 @tempfile=#<File:/var/folders/h2/9r2h6tp91t9grzhscb248__00000gn/T/RackMultipart20140312-16446-1op80uv>, @original_filename="350x150.gif", @content_type="image/gif", @headers="Content-Disposition: form-data; name=\"status[document_attributes][attachment]\"; filename=\"350x150.gif\"\r\nContent-Type: image/gif\r\n">}}, "commit"=>"Create Status"}
But this error occurs: Unpermitted parameters: attachment
This is a part of my form:
<%= f.fields_for :document do |document_fields| %>
<% document_fields.file_field :attachment %>
<% end %>
this is in my statuses_controller:
def new
@status = Status.new
@status.build_document
end
# .....
def status_params
params.require(:status).permit(:user_id, :content, :document_id, document_attributes: :document)
end
My document class:
class Document < ActiveRecord::Base
has_attached_file :attachment
end
My status class:
class Status < ActiveRecord::Base
belongs_to :user
# allowes status.document
belongs_to :document
# allowes us to save a document when we create a new status through forms
# f.fields_for :document
accepts_nested_attributes_for :document
validates :content, presence: true,
length: {minimum: 4}
validates :user_id, presence: true
end
5 Answers
Adam Sackfield
Courses Plus Student 19,663 PointsThis may be a good place to start here
Mark Lavelle
2,910 PointsI am not alone.... I have been trying the tutorial using rails 4 also I looked at the stackoverflow solution there and have noticed some difference between the way the code is structured, I think. If we follow that code structure from StackOverflow then do we not need to change the models as in document.rb belongs_ to :status status.rb has_many :documents.
Maybe my brain is fried... I have other issues relating to this section, could we set up a group for any future issues??
Adam Sackfield
Courses Plus Student 19,663 PointsMark Lavelle Google hang out may be a good thing for this. I have decided to go back to basics and get a better understanding of Ruby first and then tackle this again. But the rails api docs are a good source of info but if you dont have a grasp on what is going on in rails 3 then its hard to find a fix for rails 4.
David Wilbur
2,535 PointsI'm using Rails 4 and got it working with the following:
def status_params
params.require(:status).permit(:name, :user_id, :content, document_attributes: [:attachment] ) if params[:status]
end
Alex Romanillos
16,144 PointsDavid's solution in combination with the content type validation posted by Patte worked for me.
Thanks!
patte bläsing
331 PointsALSO you have to have this in your Document class otherwise it doesnt work. Thanks for the help Adam Sackfield
validates_attachment_content_type :attachment, :content_type => %w(image/jpeg image/jpg image/png image/gif)
Adam Sackfield
Courses Plus Student 19,663 PointsYour welcome. I am also doing this course but with Rails 4 and its a nightmare but I enjoy the struggle.
Good Luck
Mark Lavelle
2,910 PointsHave either of you tried to push the app up to Heroku yet? The user_friendship model fails i think because the Mailer is set to localhost and heroku only lets you use third party mail options it seems, any ideas as to how to change from the tutorial to say Mailchimp ( one of the heroku add ons )
Adam Sackfield
Courses Plus Student 19,663 PointsNot got that far yet. Did stage one and pushed that to heroku and had probs loading the assets and a quick search found the answers on heroku. Just google the error msg. Google is the greatest resource to learning I think. As i mentioned learning ruby before online book have started reading this. Apart from the author seems to have a screw loose its a good read.
Mark Lavelle
2,910 PointsSorry for hijacking your thread Patte I have it on likefacebook.herokuapp.com I did Michael Hartl's Ruby tutorial after running into problems with this tutorial in the beginning ( the long long ago ) If you haven't done it yet then go, now, very comprehensive, I couldn't do all the end of chapter exercises but just did what I could. Once I got a ( sort of ) handle on strong parameters, I came back to the treebook tutorial I think will post something about the heorku upload to see if jason will pick it up
Adam Kelm
29,082 PointsI also tried starting this tutorial with Ruby 2 and Rails 4. I pushed through the pain for a while, but it's just not worth it. When you're trying to learn how to use Ruby or Rails, you can't let yourself get bogged down with errors caused by who-even-knows-what, not unless you love PAIN.
For instance, early in the tutorial they use attr_accessible, which doesn't play nice with Rails 4. Much later in the tutorial, Jason brings in strong parameters, which is the Rails 4 preferred method for handling these. So you can either go through hours and hours of frustration or do it the same way the teacher does and add things as they come up.
Adam Sackfield
Courses Plus Student 19,663 PointsCouldn't agree more, I think its the reason I went grey lol. This is an invaluable free resource here You can select Rails 3.2 or 4 on the right hand side. The detail it goes into is simply awesome and also Chapter 4 will give a solid foundation to understanding Ruby. All in all I give it 10/10 a must for anybody starting on RoR, should be read before attempting the videos on here in my opinion.
Adam Sackfield
Courses Plus Student 19,663 PointsAdam Sackfield
Courses Plus Student 19,663 PointsIn the video they are using Rails 3.2 so you would need to either start over with that version, or research the new methods used in Rails 4 to perform these tasks.
For instance the attr_accessible is not needed inside the model in Rails 4 whereas in these videos they are needed.