0

I have this method in my micropost.rb, the error is souly when I run this method. When I submit my form I do not get the errors from the model but the rendering of the else condition in the controller. Something seems to be wrong in the model but don't see why I get rollback transaction from server?

The method just checks that a user has not entered and sent more than one field.

Micropost.rb

validate :return_media_field_errors

private 

def return_media_field_errors
  if :img_url? && :video_url? 
    errors.add(:img_url, "Can only submit one field at a time")
  end
end

MicropostController

def create
  @micropost = current_user.microposts.build(micropost_params)
  if @micropost.save
    flash[:success] = "Micropost created!"
    @micropost  = current_user.microposts.build
    @feed_items = current_user.feed.paginate(page: params[:page])
    render 'static_pages/home'
  else
    @feed_items = []
    render 'shared/error_messages'
  end
end

private

  def micropost_params
    params.require(:micropost).permit(:content, :picture, :picture_cache, :image_url, :video_url, :gif_url)
  end

micropost schema.rb

create_table "microposts", force: :cascade do |t|
  t.text     "content"
  t.integer  "user_id"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.string   "picture"
  t.string   "image_url"
  t.string   "video_url"
  t.string   "gif_url"
  t.index ["user_id", "created_at"], name: "index_microposts_on_user_id_and_created_at"
  t.index ["user_id"], name: "index_microposts_on_user_id"
end

View params

<div class="media_field_1">    
  Image <%= f.text_field :image_url, class: 'form-control' %>
</div>
<div class="media_field_2"> 
  Video <%= f.text_field :video_url, class: 'form-control' %>
</div> 
4
  • Seems your validation is doing its job, just perhaps not what you expected. Can you show your strong params, the submitted params and your Micropost schema? Commented Oct 4, 2017 at 6:01
  • sure, added the code and view fields. If you want the form value it is a jpg starting with https, I have a pretty heafty method on those fields to change the dimensions on a before_create action. Commented Oct 4, 2017 at 6:11
  • 1
    You have a typo img_url must be image_url. Please read stackoverflow.com/help/mcve Commented Oct 4, 2017 at 6:16
  • Oh man you be right. Commented Oct 4, 2017 at 6:17

1 Answer 1

1

I think the problem is this line:

if :img_url? && :video_url? 

here :img_url? is a symbol, not an attribute

What you want is self.image_url? or just image_url, same for video_url

Sign up to request clarification or add additional context in comments.

3 Comments

I get a nomethoderror when I change it to this on those fields.
didn't notice the typo with img. Can you try again?
Aha. You got it! Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.