Skip to content

Instantly share code, notes, and snippets.

@guinslym
Created January 4, 2014 11:26
Show Gist options
  • Save guinslym/8254408 to your computer and use it in GitHub Desktop.
Save guinslym/8254408 to your computer and use it in GitHub Desktop.
When I click submit on the form. The form doesn't load in the params the value that contains the checkbox. Matter of fact I got an error (Image shape can't be blank) due to the model
<%= form_for @image, html: { multipart: true } do |f| %>
<% if @image.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@image.errors.count, "error") %> prohibited this image from being saved:</h2>
<ul>
<% @image.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :caption %><br>
<%= f.text_field :caption %>
</div>
<div class="field">
<%= f.label :image %><br>
<%= f.file_field :image %>
</div>
<div class="field">
<%= f.label :image_shape %><br>
<%= select_tag(:image_shape, options_for_select([['landscape', 'landscape'], ['portrait', 'portrait']])) %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
class Image < ActiveRecord::Base
mount_uploader :image, ImageUploader
#validation
validates :image_shape , presence: true
end
class ImagesController < ApplicationController
before_action :set_image, only: [:show, :edit, :update, :destroy]
# GET /images
# GET /images.json
def index
@images = Image.all
end
# GET /images/1
# GET /images/1.json
def show
end
# GET /images/new
def new
@image = Image.new
end
# GET /images/1/edit
def edit
end
# POST /images
# POST /images.json
def create
@image = Image.new(image_params)
respond_to do |format|
if @image.save
format.html { redirect_to @image, notice: 'Image was successfully created.' }
format.json { render action: 'show', status: :created, location: @image }
else
format.html { render action: 'new' }
format.json { render json: @image.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /images/1
# PATCH/PUT /images/1.json
def update
respond_to do |format|
if @image.update(image_params)
format.html { redirect_to @image, notice: 'Image was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @image.errors, status: :unprocessable_entity }
end
end
end
# DELETE /images/1
# DELETE /images/1.json
def destroy
@image.destroy
respond_to do |format|
format.html { redirect_to images_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_image
@image = Image.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def image_params
params.require(:image).permit(:caption, :image, :image_shape)
end
end
@guinslym
Copy link
Author

guinslym commented Jan 4, 2014

guinslym: I assume the html name.array is wrong,.. why don't you use f.select instead of select? this results in a different scope

pduersteler, : thks I see the difference in the name attr: now (image[image_shape]) compare toname="image_shape" for the select_tag

guinslym: yep, and as you are relying on mass assignment in your controller, it won't get picked up because it is outside of your "mass assignment scope", so to say

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment