When I try to create new record image doesn't upload but when I try to edit everything working fine. No errors, after create I have nil in DB after Update I have a link to a picture and everything fine. What I did wrong? Before I used form_for and everything was working fine, then I change form_for to simple_form. Maybe problem here?
Controller:
def create
#byebug
#bar = current_user.bars.new(bar_params)
if #bar.save
UserMailer.admin_verify_email(#bar.id).deliver_now
flash[:success] = t(".bar_created_successfully")
# if admin_verified add bar to the search bos
if #bar.admin_verified
Barsearchsuggestion.index_bar(#bar)
redirect_to bars_path(my_bar: true)
else
redirect_to bar_status_user_path(current_user)
end
else
flash.now[:alert] = #bar.errors.full_messages
render 'users/add_bar'
end
end
def edit
#bar = Bar.find(params[:id])
end
def update
#bar = Bar.find(params[:id])
if #bar.update_attributes(bar_params)
flash[:success] = "Bar updated."
redirect_to bar_status_user_path(current_user)
else
flash.now[:error] = I18n.t("simple_form.error_notification.default_message")
# flash[:error] = #bar.errors.to_array
render 'edit'
end
end
private
def bar_params
params.require(:bar).permit!
end
View:
=simple_form_for(#bar) do |f|
.form-group
= f.label :profile_picture
= t(".profile_picture_ext_html")
%br
= image_tag f.object.profile_picture.bar_detail.url
= f.file_field :profile_picture, class: 'form-control'
Model:
mount_uploader :profile_picture, BarPictureUploader
You have to permit the profile_picture parameter like this:
def bar_params
params.require(:bar).permit(:profile_picture)
end
Try setting multipart: true in your form
Related
Video objects have tags (via acts-as-taggable-on gem). The update function works for all of the video attributes except for tags. I'm not sure why. Any insight would be greatly appreciated.
views/videos/_new.html.erb
<div class="">
<%= f.label(:tag_list, "Tags (separated by commas)") %></br >
<%= f.text_field :tag_list, multiple: true, class: "form-control" %>
</div>
videos_controller.rb
def edit
#video = Video.find(params[:id])
#post = #video.post
end
def update
#video = Video.find(params[:id])
#post = #video.post
#video.update_attributes(video_params)
if current_user == #video.user
if #video.save
flash[:notice] = "Video successfully updated!"
redirect_to video_path(#video)
else
flash[:notice] = "Video was not updated."
#errors = #video.errors.full_messages.join(", ")
flash[:alert] = #errors
render action: "edit"
end
else
flash[:notice] = "Only OP can edit video"
refresh :edit
end
end
private
def video_params
params.require(:video).permit(
:title,
:url,
:tag_list,
)
end
Looks like your video_params is filtering it.
Try
params.require(:video).permit(
:title,
:url,
tag_list: []
)
I am working on a rails app and making a nested resource for pictures. The model is Picture, and it is polymorphic and interacts with a couple different tables.
I am working on the controller action, and the create method. According to this answer and other things I have seen with paperclip, the params should follow the format of table_name and then file. Even when doing that, I am still getting a param is missing or the value is empty: picture error.
Here is my code:
Picture.rb
class Picture < ActiveRecord::Base
belongs_to :imageable, polymorphic: true
has_attached_file :image, style: { small: '64x64', medium: '100x100', large: '200x200' }
validates_attachment :image, presence: true, content_type: { content_type: /\Aimage\/.*\Z/ },
size: { in: 0..5.megabytes }, default_url: 'missing_img.png'
acts_as_list scope: [:imageable_id, :imageable_type]
end
pictures_controller.rb
class PicturesController < ApplicationController
before_action :authorize_user!
before_action :set_resource!
def index
#pictures = #resource.pictures
end
def create
#picture = #resource.pictures.new(picture_params) do |pic|
pic.imageable_type = #resource
pic.imageable_id = #resource.id
end
if #picture.save
redirect_to :back
flash[:success] = 'Image Saved!'
else
redirect_to :back
flash[:danger] = "#{#picture.errors.full_messages.to_sentence}"
end
end
def destroy
#picture = Picture.find(params[:id])
#resource.pictures.delete(#picture)
redirect_to :back
flash[:success] = "Picture deleted"
end
private
def set_resource!
klass = [Gym, User, Location, Product].detect { |c| params["#{c.name.underscore}_id"] }
#resource = klass.find(params["#{klass.name.underscore}_id"])
end
def picture_params
params.require(:picture).permit(:image)
end
end
pictures/index.html
<h6>Upload Pictures</h6>
<%= form_for(current_user, url: url_for(controller: 'pictures', action: 'create'), method: :post, html: { multipart: true, class: 'form-horizontal' }) do |f| %>
<%= f.file_field :image, type: :file, multiple: true, style: 'padding-bottom: 25px;' %>
<%= f.submit "Upload", class: "btn btn-gen" %>
<% end %>
And here is the param request on form submit:
{"utf8"=>"✓",
"authenticity_token"=>"+fsA6liECF7pkUp/0BA0wDHq9Vv63jB+WBb7O/uUEDhhmIOZ22Rb1rNWDwuwPTDPNS7jg7vP/fVCVllDV21wDw==",
"user"=>{"image"=>[#<ActionDispatch::Http::UploadedFile:0x007f87b138acf8 #tempfile=#<Tempfile:/var/folders/9c/1_0mk00n297f560_fpv9jzl40000gn/T/RackMultipart20161020-25582-3qt3gc.jpg>,
#original_filename="b4lROOR.jpg",
#content_type="image/jpeg",
#headers="Content-Disposition: form-data; name=\"user[image][]\"; filename=\"b4lROOR.jpg\"\r\nContent-Type: image/jpeg\r\n">]},
"commit"=>"Upload",
"user_id"=>"15"}
Even with this, I am still getting the error. Does anyone see anything wrong I'm doing in my code that I could fix? Any help is much appreciated.
The problem is that your picture_params method requires the route param key picture while your form has the key user.
Lets do some refactoring:
private
def set_resource!
#resource = resource_class.find(params[param_key + "_id"])
end
def resource_class
#resource_class ||= [Gym, User, Location, Product].detect do |c|
params[ param_key(c) + "_id" ]
end
end
def param_key(klass = resource_class)
ActiveModel::Naming.param_key(klass)
end
def picture_params
params.require(param_key).permit(:image)
end
Instead of klass.name.underscore we use ActiveModel::Naming which is how rails figures out how to use models for stuff like routes, params or translations.
I figured out a temporary work around. Definitely not ideal, and if anyone has a better suggestion I would love to hear it. But here is what I currently have that does work.
def create
pic = nil
if params[:user]['image']
params[:user]['image'].each do |image|
pic = #resource.pictures.create(image: image, imageable_type: #resource, imageable_id: #resource.id)
end
if pic.save
redirect_to :back
flash[:success] = 'Image Saved!'
else
redirect_to :back
flash[:danger] = "#{pic.errors.full_messages.to_sentence}"
end
else
flash[:danger] = "Picture file not found"
redirect_to :back
end
end
how to insert parameter value manually from controller,
this my controller
def new
#new_post = Post.new(params[:title])
end
def create
#new_post = Post.new(new_post_params)
if #new_post.save
flash[:success] = 'Post created!'
redirect_to home_url
else
flash[:danger] = #new_post.errors.full_messages.to_sentence
redirect_to home_url
end
end
private
def new_post_params
params.require(:post).permit(
:title,
poster_id: current_user.id,
poster_name: current_user.name
)
end
my form view like this
form_for #new_post do |f|
f.text_area :title
end
tired using this method, poster_id and poster_name still blank
def new_post_params
params.require(:post).permit(
:title,
poster_id: current_user.id,
poster_name: current_user.name
)
end
Try this
params.require(:post).permit(:title).merge(
{
poster_id: current_user.id,
poster_name: current_user.name
}
)
def new_post_params
params[:post][:poster_id] = current_user.id
params[:post][:poster_name] = current_user.name
params.require(:post).permit(
:title,
:poster_id,
:poster_name
)
end
My app is using Rails 4.1.8 and I use Simple Form gem.
I get the undefined local variable or method error on the strong parameter definition under private and I don't understand the cause of this.
_form.haml.html
= simple_form_for #recipe, html: { multipart: true } do |f|
- if #recipe.errors.any?
#errors
%h2
= pluralize(#recipe.errors.count, "error")
prohibited this recipe from being saved
%ul
- #recipe.errors.full_messages.each do |msg|
%li= msg
= f.input :title
= f.input :serving
= f.input :prep
= f.input :cook
= f.button :submit
controller
class RecipesController < ApplicationController
def create
#recipe = Recipe.new(recipe_params)
if #recipe.save
redirect_to #recipe, notice: "Successfully created"
else
render 'new'
end
private
def recipe_params
params.require(:recipe).permit(:title, :serving, :prep, :cook)
end
end
end
And the submit button takes me to index route http://localhost:3000/recipes rather than
show http://localhost:3000/recipes/1
You didn't close create method properly. It should be:
class RecipesController < ApplicationController
def create
#recipe = Recipe.new(recipe_params)
if #recipe.save
redirect_to #recipe, notice: "Successfully created"
else
render 'new'
end
end
private
def recipe_params
params.require(:recipe).permit(:title, :serving, :prep, :cook)
end
end
I trying write params to Company model. But I have error undefined method `model_name' for NilClass:Class in this point = simple_form_for #company, url: update_settings_company_path do |f|. Where I must set #company?
Controller
def change_settings
#vacation_days = current_company.vacation_days
#illnes_days = current_company.illnes_days
end
def update_settings
if #company.update(company_params)
redirect_to account_company_path, notice: t('company.settings_changed')
else
render action: 'change_settings'
end
end
private
def company_params
params.require(:company).permit(:vacation_days, :illnes_days)
end
View
.company_settings
= simple_form_for #company, url: update_settings_company_path do |f|
= f.error_notification
= f.input :vacation_days
= f.input :illnes_days
%br
= f.submit t('common.save'), class: 'btn'
= link_to t('common.back'), account_company_path, class: 'btn'
routes
resource :company, only: :all do
get :change_settings
post :update_settings
patch :update_settings
end
What's wrong? Help me please
You don't set #company instance variable in both your actions. You can do it using before_filter, like this:
before_filter :find_company
def change_settings
#vacation_days = current_company.vacation_days
#illnes_days = current_company.illnes_days
end
def update_settings
if #company.update(company_params)
redirect_to account_company_path, notice: t('company.settings_changed')
else
render action: 'change_settings'
end
end
private
def company_params
params.require(:company).permit(:vacation_days, :illnes_days)
end
def find_company
#company = current_company
end
Try this instead, You need to set the instance variable before you use it. By default the instance variable is set to nil.
def update_settings
#company = current_company
if #company.update(company_params)
redirect_to account_company_path, notice: t('company.settings_changed')
else
render action: 'change_settings'
end
end