simple_hashtag icon indicating copy to clipboard operation
simple_hashtag copied to clipboard

Can't mass-assign protected attributes: name

Open SqueezedLight opened this issue 11 years ago • 12 comments

Hallo, i installed the gem and included simple_hashtag in my post model.

When i try to create a post (via ajax) i get a mass assignment error while saving.

Started POST "/microposts" for 127.0.0.1 at 2013-12-03 22:15:04 +0100
Processing by MicropostsController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"chPGHygVCT5O1+jRxAFWvVV1uii8OCrFTPDXChO6jck=", "micropost"=>{"content"=>"#Test"}, "commit"=>"Create Post"}
  User Load (0.8ms)  SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'XQUBJG1KFAjMl4-JR9Ym6g' LIMIT 1
   (0.5ms)  BEGIN
  SimpleHashtag::Hashtag Load (1.0ms)  SELECT "simple_hashtag_hashtags".* FROM "simple_hashtag_hashtags" WHERE (lower(name) ='test') LIMIT 1
   (0.7ms)  ROLLBACK
Completed 500 Internal Server Error in 616ms

The exact error message is:

ActiveModel::MassAssignmentSecurity::Error in MicropostsController#create
Can't mass-assign protected attributes: name

Do you know what i'm doing wrong? I'm really lost here, every hint is appreciated.

Thanks a lot, Tom

SqueezedLight avatar Dec 03 '13 21:12 SqueezedLight

I don't want to add

attr_accessible :name

to hashtag.rb unless it is absolutely required. But others managed to get it to work without this line. So i think the problem is my configuration...

SqueezedLight avatar Dec 04 '13 12:12 SqueezedLight

Hi @SqueezedLight

Indeed it's not supposed to be needed.

Can you post the concerned controller, model and view, see if I can help ?

ralovely avatar Dec 04 '13 12:12 ralovely

OMG, maybe you don't believe it, but you solved my problem. I was just posting all my code into this github window, when i realized a copy/paste error in the form which creates my posts.

Without you i have never thought that my problem could lie in the posts view. I was so fixated on my models...

Thanks for your time and keep the good work going.

Best regards Tom

SqueezedLight avatar Dec 04 '13 13:12 SqueezedLight

I'm getting the same error. Code works fine when I make a post without a hashtag. Here is my controller, model and view:

class PostsController < ApplicationController

  def create
    @post = Post.new(params[:post])
    @post.owner = @visitor.name
    @post.save!
    flash[:success] = 'Post added successfully'
    redirect_to posts_path
  end

  def index
    @posts = Post.all
    @post = Post.new
  end

end

class Post < ActiveRecord::Base
  include SimpleHashtag::Hashtaggable
  attr_accessible :message, :owner, :pic
  hashtaggable_attribute :message

  validates :message, presence: true
  has_attached_file :pic, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "1.png"
  validates_attachment :pic,
    :content_type => { :content_type => ["image/jpg", "image/jpeg", "image/gif", "image/png"] }
  validates_attachment_size :pic, :less_than => 5.megabytes

end

<%= form_for(post, html: {multipart: true}) do |f| %>
  <div class="image-upload">
    <label for='post_pic'>
      <img src='http://www.clker.com/cliparts/3/3/6/4/12074316411296807266camera%20white.svg.med.png' height="60" class="img_upload" />
    </label>
    <%= f.file_field :pic %>
  </div>
  <%= f.text_field :message %>
  <%= f.submit %>
<% end %>

strykerapps avatar Jan 09 '14 16:01 strykerapps

Forgot to mention: please take a look if you have a chance. I really appreciate your help.

strykerapps avatar Jan 09 '14 16:01 strykerapps

Make sure you have all the files. When I installed the gem a few files were left out like /lib/simple_hashtag/hashtabble.rb.

Hope you can fix your error message, because this gem is really great.

licatajustin avatar Jan 09 '14 18:01 licatajustin

@licatajustin Thanks for the response. Looks like I have all the files. This gem looks like it does exactly what I need for my app, but I'm clueless about that error. Strongly considering going with

attr_accessible :name

but then I'd have to deal with the downcasing. Also, I want to do it the 'right' way...

strykerapps avatar Jan 09 '14 18:01 strykerapps

I would also be very interested in a solution for this problem. I once "solved" this problem (see above). But a few days ago, it occured again. Dont know whats the reason for this...

SqueezedLight avatar Jan 09 '14 19:01 SqueezedLight

Hi @SqueezedLight, @makoplasty

Well, this is a bummer. The hashtags never transit through the controller, they are set in the background, and should not be impaired with mass assignment issue.

The main problem I have is I can't replicate. I first suspected a Rails 3/4 issue since there are changes in the MassAssignement behaviour, but I have both a 3.1 and a 4.0 app working with it.

I will try and replicate some more over the week end. If one of you is able to send me failing code (given it's quick enough to setup), I can look at it.

@makoplasty, if you add the attr_accessible it should be in the Gem (in /lib/simple_hashtag/hashtag.rb), not in your model: your model doesn't have the tag stored in it. (you can find the gem with bundle open simple_hashtag)

ralovely avatar Jan 10 '14 06:01 ralovely

Hi @ralovely

I'm going to add you as a private collaborator to the repository. The app is still very much in progress, but it may be helpful for replication.

Thank you & have a great weekend, Tamas

strykerapps avatar Jan 10 '14 18:01 strykerapps

+1 on this issue –

My setup is nearly identical to @makoplasty's.

For the time being, I'll fork a variation with attr_accessible :name – however, would love to see this resolved.

colewinans avatar Jan 30 '14 16:01 colewinans

Facing the same issue Can't mass-assign protected attributes: name. Forked a variation to resolve mass assignment issue for name.

Aastha-Tangri avatar Dec 14 '14 14:12 Aastha-Tangri