Unpermitted parameter for photos - ruby-on-rails-4

I'm on rails 4 and using carrierwave, and I'm trying let users upload photos that belong to specific objects. The parent object is Property, and the child object is Photo. I did this with an eye towards allowing a Property to have multiple photos.
I would like to be able to upload a photo while on the Property edit page. The uploaded photo will have a foreign key that relates to the property_id so I can identify which property it belongs to (hence the hidden field).
My problem is that I keep getting an "Unpermitted parameter: photos" message in my logs that's keeping me from creating the Photo object.
My form looks like this
<%= form_for #property, html: { multipart: true } do |f| %>
<%= render "property_form_fields", f: f %>
<%= f.fields_for :photos do |photo| %>
<p>Add photos:</p>
<%= photo.file_field :photo_file_name%>
<%= photo.hidden_field :property_id, :value => #property.id%>
<% end %>
<%= f.submit "Update property", class: 'btn btn-default' %>
<% end %>
Photo model:
class Photo < ActiveRecord::Base
belongs_to :property
mount_uploader :photo_file_name, PhotoUploader
validate :picture_size
private
# Validates the size of an uploaded picture.
def picture_size
if picture.size > 5.megabytes
errors.add(:picture, "should be less than 5MB")
end
end
end
Property model
class Property < ActiveRecord::Base
has_many :photos
end
Here is my Property Controller
def edit
#property = Property.find(params[:id])
#photos = Photo.where(:property_id => #property.id)
end
def update
#property = Property.find(params[:id])
if #property.update_attributes(property_params)
flash[:success] = "Property was successfully updated."
redirect_to properties_path
else
flash[:error] = "Property was not created"
render edit_property_path
end
private
def property_params
params.require(:property).permit(:property_name, photos_attributes: [:id, :photo_file_name,:property_id])
end
end
Here are my logs when I try to add a photo
Started PATCH "/properties/19" for 108.41.84.27 at 2016-07-08 19:59:24 +0000
Processing by PropertiesController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"ucP...", "property"=>{"property_name"=>"abc", "photos"=>{"photo_file_name"=>#<ActionDispatch::Http::UploadedFile:0x007fe11f0a5598 #tempfile=#<Tempfile:/home/ubuntu/workspace/RackMultipart20160708-6915-1havz2i.jpg>, #original_filename="1554439_686460264746128_2054601640279902551_n.jpg", #content_type="image/jpeg", #headers="Content-Disposition: form-data; name=\"property[photos][photo_file_name]\"; filename=\"1554439_686460264746128_2054601640279902551_n.jpg\"\r\nContent-Type: image/jpeg\r\n">, "property_id"=>"19"}}, "commit"=>"Update property", "id"=>"19"}
Manager Load (0.3ms) SELECT "managers".* FROM "managers" WHERE "managers"."id" = ? ORDER BY "managers"."id" ASC LIMIT 1 [["id", 1]]
Property Load (0.2ms) SELECT "properties".* FROM "properties" WHERE "properties"."id" = ? LIMIT 1 [["id", 19]]
Unpermitted parameter: photos
(0.1ms) begin transaction
(0.1ms) commit transaction
Redirected to https://project-apple-mcl282.c9users.io/properties
Completed 302 Found in 12ms (ActiveRecord: 0.7ms)

your Property model needs to accept nested attributes for photos:
class Property < ActiveRecord::Base
has_many :photos
accepts_nested_attributes_for :photos
end

Related

Remove uploaded images rails 4 carrierwave

Trying to remove uploaded images using carrierwave
<%= f.fields_for :images do |ff| %>
<div class="form-group">
<label>
<%= ff.check_box :remove_image %>
<%= image_tag ff.object.image %>
</label>
</div>
<% end %>
Getting such params in controller
"images_attributes"=>{"0"=>{"remove_image"=>"0", "id"=>"13"}, "1"=>{"remove_image"=>"1", "id"=>"14"}, "2"=>{"remove_image"=>"0", "id"=>"15"}, "3"=>{"remove_image"=>"0", "id"=>"16"}, "4"=>{"remove_image"=>"0", "id"=>"17"}, "5"=>{"remove_image"=>"0", "id"=>"18"}}}
But when updating an object with these params nothing happens, what am I missing?
update
def update
#country = Country.find(params[:id])
if #country.update(country_params)
flash[:notice] = 'Country is successfully updated.'
redirect_to edit_admin_country_path
else
flash[:error] = #country.errors.full_messages[0]
render 'edit'
end
end
def country_params
permitted = [{images_attributes: ["image", "#original_filename", "#content_type", "#headers", "_destroy", "id", "remove_image"]}]
params.require(:country).permit(*permitted)
end
class Country < ActiveRecord::Base
has_many :images
....
end
class Image < ActiveRecord::Base
mount_uploader :image, ImageUploader
belongs_to :country
end
your form looks good but you are missing the controller action
mine looks like:
class ImageController < ApplicationController
...
def update
#image = Image.find(params[:id])
...
if params[:images][:remove_image].present?
#image.remove_image!
end
#image.save
end
end
If you want to remove the file manually, you can call remove_avatar!, then save the object.

Act-As-Taggable not saving to database

Hi I am attempting to use act_as_taggable ruby gem and have followed the github guide and a recent tutorial as well as checked stack overflow for advice. My code all seems to be correct but when I attempt to save tags via my form partial or via the rails console they do not save to the database.
Below relevant code excerpts.
Thanks in advance for any help!
post.rb
class Post < ActiveRecord::Base
acts_as_taggable
acts_as_taggable_on :tags
validates :title, presence: true, length: {minimum: 5}
validates :body, presence: true
extend FriendlyId
friendly_id :title, use: :slugged
end
post_controller.rb
class PostsController < ApplicationController
before_action :find_post, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
def index
if params[:tag].present?
#posts = Post.tagged_with(params[:tag]).paginate(:page => params[:page], :per_page => 10)
else
#posts = Post.all.order('created_at DESC').paginate(page: params[:page], per_page: 10)
end
end
def new
#post = Post.new
end
def show
end
def create
#post = Post.new(post_params)
if #post.save
redirect_to #post, notice: "Article succesfully saved!"
else
render 'new', notice: "Try Again. I was unable to save your post."
end
end
def edit
end
def update
if #post.update(params[:post].permit(:title, :body))
redirect_to #post, notice: "Article succesfully edited!"
else
render 'edit'
end
end
def destroy
#post.destroy
redirect_to posts_path
end
private
def post_params
params.require(:post).permit(:title, :body, :slug, :tag_list => [])
end
def find_post
#post = Post.friendly.find(params[:id])
end
end
routes.rb
Rails.application.routes.draw do
devise_for :users, :skip => :registrations
root 'pages#home'
resources :posts
resources :portfolios
get 'tags/:tag', to: 'posts#index', as: :tag
end
_form.html.erb
<%= form_for #post do |f| %>
<% if #post.errors.any? %>
<div id="errors">
<h2><%= pluralize(#post.errors.count, "error") %> prevented this post from saving:</h2>
<ul>
<% #post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.label :title %><br>
<%= f.text_field :title, class: 'form-control form-control-lg' %><br>
<br>
<%= f.label :body %><br>
<%= f.text_area :body, class: 'form-control form-control-lg', :rows => "10" %><br>
<br>
<%= f.label :tag_list, "Tags (separated by commas)" %>
<%= f.text_field :tag_list, class: 'form-control' %>
<br>
<%= f.submit %>
<% end %>
excerpt from show.html.erb
<% #post.tags.any? %>
<% #post.tags.each do |tag| %>
<li><a href="#">
<%= link_to tag.name, tag_path(tag.name) %>
</a></li>
<% end %>
Params log for _form as requested by Pavan:
Started PATCH "/posts/my-third-post-with-tags" for ::1 at 2015-09-23 15:56:36 +1000
Processing by PostsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"GwLMImKSDyhPt77nI+fhIkF4QVsFxMen851wjcVorrxOCdU+TcRKIZv1im4/oNx7DERYCI91vE9YDjvSda7MAg==", "post"=>{"title"=>"My third post with tags", "body"=>"to update your local repository to the newest commit, execute\r\ngit pull\r\nin your working directory to fetch and merge remote changes.\r\nto merge another branch into your active branch (e.g. master), use\r\ngit merge <branch>\r\nin both cases git tries to auto-merge changes. Unfortunately, this is not always possible and results in conflicts. You are responsible to merge those conflicts manually by editing the files shown by git. After changing, you need to mark them as merged with\r\ngit add <filename>\r\nbefore merging changes, you can also preview them by using\r\ngit diff <source_branch> <target_branch>", "tag_list"=>"tag, css"}, "commit"=>"Update Post", "id"=>"my-third-post-with-tags"}
Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."slug" = ? ORDER BY "posts"."id" ASC LIMIT 1 [["slug", "my-third-post-with-tags"]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Unpermitted parameter: tag_list
(0.0ms) begin transaction
(0.0ms) commit transaction
Redirected to http://localhost:3000/posts/my-third-post-with-tags
Completed 302 Found in 4ms (ActiveRecord: 0.3ms)
Started GET "/posts/my-third-post-with-tags" for ::1 at 2015-09-23 15:56:36 +1000
Processing by PostsController#show as HTML
Parameters: {"id"=>"my-third-post-with-tags"}
Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."slug" = ? ORDER BY "posts"."id" ASC LIMIT 1 [["slug", "my-third-post-with-tags"]]
ActsAsTaggableOn::Tag Exists (0.1ms) SELECT 1 AS one FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = ? AND "taggings"."taggable_type" = ? AND "taggings"."context" = ? LIMIT 1 [["taggable_id", 2], ["taggable_type", "Post"], ["context", "tags"]]
ActsAsTaggableOn::Tag Load (0.1ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = ? AND "taggings"."taggable_type" = ? AND "taggings"."context" = ? [["taggable_id", 2], ["taggable_type", "Post"], ["context", "tags"]]
Rendered posts/show.html.erb within layouts/application (3.2ms)
Rendered layouts/_header.html.erb (496.3ms)
Rendered devise/shared/_links.html.erb (0.2ms)
Rendered devise/sessions/_new.html.erb (4.6ms)
Rendered layouts/_slideoutpanels.html.erb (460.2ms)
Rendered layouts/_footer.html.erb (0.0ms)
Completed 200 OK in 1486ms (Views: 1484.3ms | ActiveRecord: 0.2ms)
Your error is from PostsController#update as HTML
In controller you don;t permit tag_list
#post.update(params[:post].permit(:title, :body))

link to update model parameter yields missing or empty value error

I'm trying to give provide a series of links that when selected will update a corresponding user's parameter to that which is supplied by the link without rendering a form. The only problem I'm running into is that when it calls the update method I get: param is missing or the value is empty: user.
update method
def update
#user = User.find(params[:id])
if #user.update(user_params)
redirect_to list_path
flash[:success] = "User updated"
else
redirect_to list_path
flash[:alert] = "User not updated"
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation, :title, :role, :address, :phone)
end
link_to
<% User.all.each do |user| %>
<% if user.role == "new" %>
<tr>
<td><%= user.name %></td>
<td><%= user.title %></td>
<td><%= user.role %></td>
<td><%= link_to "Admin", update_user_path(id: user.id, :role => "admin"), :method => :put %> / <%= link_to "Moderator", '#' %> / <%= link_to "Member", '#' %> / <%= link_to "Other", '#' %></td>
</tr>
<% end %>
<% end %>
param.inspect
{"_method"=>"put",
"authenticity_token"=>"AUcV1swtGmRuDelTzLkbLd9Gmj6+phnHaSFRqrvDyETLoxNUQSaAgkes4ViWXoAZ33K5NZojVz9XdgIIsqJblA==",
"id"=>"2",
"role"=>"admin"}
From what I've found the params.inspect should have something like [:user] => before all the parameters listed, but I cannot figure out why it is not nor can I successfully find a way to clarify.
error:
param is missing or the value is empty: user
def user_params
**params.require(:user).permit(:name, :email, :password,**
:password_confirmation, :title, :role, :address, :phone)
end
end
Log after link select
Started PUT "/update_user?id=4&role=admin" for 97.78.175.155 at 2015-06-30 15:51:47 +0000
Cannot render console from 97.78.175.155! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by UsersController#update as HTML
Parameters: {"authenticity_token"=>"m+LcLOBL5NBXHQXD19o45/iV07/V1HiAXpykV+NiXIpRBtqubUB+Nn68DciNPaPT+KHwtPFRNnhgy/f16gPPWg==", "id"=>"4", "role"=>"admin"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 4]]
Unpermitted parameters: _method, authenticity_token, id
(0.2ms) begin transaction
User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE (LOWER("users"."email") = LOWER('qwerty#poster.com') AND "users"."id" != 4) LIMIT 1
(0.1ms) rollback transaction
Redirected to https://rails-tutorial-hougthonbrad.c9.io/list
Completed 302 Found in 11ms (ActiveRecord: 0.7ms)
Started GET "/list" for 97.78.175.155 at 2015-06-30 15:51:47 +0000
Cannot render console from 97.78.175.155! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by UsersController#list as HTML
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
User Load (0.5ms) SELECT "users".* FROM "users"
Rendered users/list.html.erb within layouts/application (3.4ms)
Completed 200 OK in 172ms (Views: 165.1ms | ActiveRecord: 0.7ms)
Change your update action to the below.
def update
#user = User.find(params[:id])
if #user.update(user_params)
redirect_to list_path
flash[:success] = "User updated"
else
redirect_to list_path
flash[:alert] = "User not updated"
end
end
Update #1:
Looking at your params, you need to change your user_params like the below.
def user_params
params.require(:user).permit(:id, :name, :email, :password, :password_confirmation, :title, :role, :address, :phone)
end
Update #2:
Try changing your link_to to the below
<%= link_to "Admin", update_user_path(user, user: {:id => user.id, :role => "admin"}), :method => :put %>
You have params.require(:user) within your user_params.
That user hash is usually generated by form_for.
I would suggest to render a form with all fields hidden except the submit button for your use case.

trying to share a model in my app

I am creating a website using a rails and I would like to share the model. I followed The tutorial for creating an article. In the ArticlesController is index, show, new, edit, create, update and destroy. I figured I would use this as the data entry part of my app. Then I was going to create a PageController that has home, news, videos, music and events. Here is my set up:
here is my Article model
class Article < ActiveRecord::Base
has_attached_file :image, styles: { large:"700x700>", medium:"300x300>", thumb:"150x150#"}
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
validates :title, presence: true,
length: { minimum: 5 }
belongs_to :page
end
here is my page model
class Page < ActiveRecord::Base
has_many :article
end
here is my page controller
class PageController < ApplicationController
def home
#page = Page.includes(:articles)
end
def news
end
def videos
end
def music
end
def events
end
end
here is my home view
<h1>Page#home</h1>
<p>Find me in app/views/page/home.html.erb</p>
<%= #page.articles.each do |article| %>
<%= article.title %>
<%= article.content %>
<%= article.image %>
<% end %>
here is the error I get
undefined method `articles' for #<ActiveRecord::Relation []>
not clear onwhats going on. Any help would be appreciated
From the limited information you posted, I am assuming the best thing for you to do is create an association. Pages should has_many articles and articles should belong_to a page. http://guides.rubyonrails.org/association_basics.html
After that is setup, your controller can be set up as follows:
#page = Page.includes(:articles)
This will load your pages and its related articles in a single SQL query, then you can call
#page.articles.each |article|
puts article.title
puts article.content
puts article.image
end
You can now access all the information and use it as you please.

Can't Save Image Attributes with Paperclip in Rails 4

I have two associated models in my Rails 4 app: product.rb and image.rb. The Image model allows attached files using the Paperclip gem.
Images belong_to a Product, and a product has_many Images.
I would like to use the Product's new view to create and attach an image when I create a product. Each time I try, the parameters associated with the Paperclip image do not get saved, and end up nil.
Here are the models:
Product.rb
class Product < ActiveRecord::Base
validates :name, :part_number, presence: true
has_many :images, dependent: :destroy
belongs_to :category
accepts_nested_attributes_for :images, allow_destroy: true
end
Image.rb
class Image < ActiveRecord::Base
belongs_to :product
has_attached_file :image
end
Looking through past Stack Overflow questions I've found some relevant questions and answers, but none that seem to help my particular situation. So far I'm able to submit the file with the correct attributes, but it doesn't save them to the database.
ProductsController#create
def create
#product = Product.new(product_params)
end
def product_params
params.require(:product).permit(:name,
:category_id,
:part_number,
images_attributes: [:image])
end
ProductsController#new
def new
#product = Product.new
#categories = # irrelevant to question
end
products/new.html.haml
= form_for #product, :html =>
= f.label :name
= f.text_field :name
= f.label :part_number
= f.text_field :part_number
= f.label :category_id
= f.select :category_id, #ca
= f.fields_for :image do |ff|
= ff.label :image
= ff.file_field :image
= f.submit('Create Product')
Looking at the parameters I can tell that the correct attributes are being passed on:
Example parameters:
{"utf8"=>"✓",
"authenticity_token"=>"jGNy/TasHzP0EcWDFzZ3XH5/fpxb6vD+ncQ2PZSQ3/I=",
"product"=>{"name"=>"bar",
"part_number"=>"foo",
"category_id"=>"30",
"image"=>{
"image"=>#<ActionDispatch::Http::UploadedFile:0x007fc82f58e0e0
#tempfile=#<Tempfile:/var/folders/04/23d9grkj5fv3wkj2t8858kx40000gn/T/RackMultipart20131029-64949-1ldkz4g>,
#original_filename="FPE230_b.jpg",
#content_type="image/jpeg",
#headers="Content-Disposition: form-data; name=\"product[image][image]\"; filename=\"FPE230_b.jpg\"\r\nContent-Type: image/jpeg\r\n">}},
"commit"=>"Create Product"}
However, the image is not actually being saved to the database. How can I rectify this?
EDIT
Looking more closely at the logs, I see that I'm getting an "unpermitted parameters: image" error. This is even after adding images_attributes: [:image] to my product_params method.
I was able to get a setup similar to yours working with the following changes:
In ProductsController#create, instead of building the image separately, let the nested attributes handle it and just do:
#product = Product.new(product_params)
And allow the nested parameters (I figured out the image_attributes: [:image] from this question):
def product_params
params.require(:product).permit(:name, :part_number, images_attributes: [:image])
end
This is what my parameters look like in the logs for a create request:
{
"utf8"=>"✓",
"authenticity_token"=>"7EsPTNXu127itgp4fbohu672/L4XnSwLEkrqUGec3pY=",
"product"=>{
"name"=>"whatever",
"part_number"=>"12345",
"images_attributes"=>{
"0"=>{
"image"=>#<ActionDispatch::Http::UploadedFile:0x007feb0c183b08
#tempfile=#<Tempfile:/var/folders/6k/16k80dds0ddd6mhvs5zryh3r0000gn/T/RackMultipart20131031-51205-emaijs>,
#original_filename="some-image.png",
#content_type="image/png",
#headers="Content-Disposition: form-data; name=\"product[images_attributes][0][image]\"; filename=\"ponysay.png\"\r\nContent-Type: image/png\r\n">
}
}
},
"commit"=>"Create"}
And I see an insert into products and an insert into images.
If that doesn't work, could you post your ProductsController#new and your new product form?
EDIT AFTER YOUR EDIT:
I have 3 different things in my app/views/products/new.html.erb and ProductsController#new that might be affecting your results:
In the form_for, I have multipart: true as in the paperclip documentation:
<%= form_for #product, :url => products_path, :html => { :multipart => true } do |f| %>
Because Product has_many :images, in my form I have fields_for :images instead of fields_for :image:
<%= f.fields_for :images do |i| %>
<%= i.file_field :image %>
<% end %>
This fields_for doesn't show anything on the page unless in the ProductsController#new I build an image; does your Product.new do this by any chance?
def new
#product = Product.new
#product.images.build
end
Are there any particular reasons for these differences you have? Does your code work if you change it to match mine?