Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't mass-assign protected attributes: name #4

Open
SqueezedLight opened this issue Dec 3, 2013 · 12 comments
Open

Can't mass-assign protected attributes: name #4

SqueezedLight opened this issue Dec 3, 2013 · 12 comments

Comments

@SqueezedLight
Copy link

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
Copy link
Author

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

@ralovely
Copy link
Owner

ralovely commented Dec 4, 2013

Hi @SqueezedLight

Indeed it's not supposed to be needed.

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

@SqueezedLight
Copy link
Author

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

@strykerapps
Copy link

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
Copy link

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

@licatajustin
Copy link

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.

@strykerapps
Copy link

@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...

@SqueezedLight
Copy link
Author

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 SqueezedLight reopened this Jan 9, 2014
@ralovely
Copy link
Owner

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)

@strykerapps
Copy link

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

@colewinans
Copy link

+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.

@Aastha-Tangri
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants