I have two models :
Post and Picture
class Post < ActiveRecord::Base
has_many :picture, :dependent => :destroy
accepts_nested_attributes_for :picture, :allow_destroy => true, :reject_if => lambda { |t| t['pictures'].nil? }
end
and
class Picture < ActiveRecord::Base
belongs_to :posts
has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
end
Picture has a paperclip attachment : image
I created a nested form but i'm not able to know if my picture is saved correctly since i can't display in the show page.
Here is my form :
<%= form_for #post, html: { multipart: true} do |f| %>
<% if #post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% #post.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.label :brand %><br>
<%= f.text_field :brand %>
</div>
<div class="field">
<%= f.label :model %><br>
<%= f.text_field :model %>
</div>
<div class="field"><%= f.fields_for :picture do |p| %>
<%= p.file_field :image %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
My show page :
<p id="notice"><%= notice %></p>
<%= #post.picture.each do |pic| %>
<%= image_tag pic.image.url(:medium) %>
<% end %>
<p>
<strong>Title:</strong>
<%= #post.title %>
</p>
<p>
<strong>Description:</strong>
<%= #post.description %>
</p>
<p>
<strong>Brand:</strong>
<%= #post.brand %>
</p>
<p>
<strong>Model:</strong>
<%= #post.model %>
</p>
<%= link_to 'Edit', edit_post_path(#post) %> |
<%= link_to 'Back', posts_path %>
The post controller :
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
# GET /posts
# GET /posts.json
def index
#posts = Post.all
end
# GET /posts/1
# GET /posts/1.json
def show
end
# GET /posts/new
def new
#post = Post.new
#post.picture.build
end
# GET /posts/1/edit
def edit
end
# POST /posts
# POST /posts.json
def create
#post = Post.new(post_params)
respond_to do |format|
if #post.save
if params[:image]
params[:image].each { |image|
#post.picture.create(image: image)
}
end
format.html { redirect_to #post, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: #post }
else
format.html { render :new }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if #post.update(post_params)
format.html { redirect_to #post, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: #post }
else
format.html { render :edit }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
#post.destroy
respond_to do |format|
format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
#post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:title, :description, :brand, :model, picture_attributes: [ :id, :post_id, :image, :_destroy])
end
end
When i heat create post, i have no error the post is rendered but instead of having the image displayed i have this : "[]" ?
Terminal output :
Processing by PostsController#show as HTML
Parameters: {"id"=>"8"}
Post Load (0.4ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", 8]]
Picture Load (0.3ms) SELECT "pictures".* FROM "pictures" WHERE "pictures"."post_id" = ? [["post_id", 8]]
Is there something i did wrong ?
The first issue you have is that you've referenced your associations incorrectly:
#app/models/post.rb
class Post < ActiveRecord::Base
has_many :pictures #-> plural
end
#app/models/picture.rb
class Picture < ActiveRecord::Base
belongs_to :post #-> singular
has_attached_file :image
validates :image, content_type: { content_type: /\Aimage\/.*\Z/ }
end
You can see about the association names here:
A pro tip is to use the paperclip_defaults options in your application.rb:
#config/application.rb
...
config.paperclip_defaults: {
styles: { medium: "300x300>", thumb: "100x100>" },
default_url: "/images/:style/missing.png"
}
This will allow you to set the "defaults" for all implementations of Paperclip in your app, which can be overridden as you need in each model. Just makes it less messy I find...
In regards your error, you have all the pieces in place; I think the issue is your association names (see above).
This is what I would have:
#app/controllers/posts_controller.rb
class PostsController < ApplicationController
def new
#post = Post.new
#post.pictures.build #-> plural
end
def create
#post = Post.new post_params
#post.save
end
private
def post_params
params.require(:post).permit(:title, :description, :brand, :model, picture_attributes: [:image, :_destroy])
end
end
You'd then be able to use the following form:
#app/views/posts/new.html.erb
<%= form_for #post, multipart: true do |f| %>
<%= f.fields_for :pictures do |p| %>
<%= p.file_field :image %>
<% end %>
<%= f.submit %>
<% end %>
--
This should submit to your db properly; to display the images, you can use the following:
#app/views/posts/show.html.erb
<% #post.pictures.each do |picture| %>
<%= image_tag picture.image.url %>
<% end %>
Related
I'm very new to Rails development and having a problem saving multiple images/attachments to a model. My problem is that the code below is not actually saving to the item_images table when I submit the form. I am following This Article as a guide, though it seems to be a bit out of date. I feel I'm in a little over my head at this point so I hope someone can point out what I'm missing. Thanks!
I have the following models:
item.rb
class Item < ActiveRecord::Base
has_many :item_images, :dependent => :destroy
accepts_nested_attributes_for :item_images, :reject_if => lambda { |t| t['item_image'].nil? }
end
item_image.rb
class ItemImage < ActiveRecord::Base
belongs_to :item
has_attached_file :image,
:styles => { thumb: "100x100#", small: "400x400#", large: "700x700" }
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
end
My controller looks like this:
items_controller.rb
class ItemsController < ApplicationController
before_action :set_item, only: [:show, :edit, :update, :destroy]
# GET /items
# GET /items.json
def index
#items = Item.all
end
# GET /items/1
# GET /items/1.json
def show
end
# GET /items/new
def new
#item = Item.new
4.times {#item.item_images.build}
end
# GET /items/1/edit
def edit
4.times {#item.item_images.build}
end
# POST /items
# POST /items.json
def create
#item = Item.new(item_params)
respond_to do |format|
if #item.save
format.html { redirect_to #item, notice: 'Item was successfully created.' }
format.json { render :show, status: :created, location: #item }
else
format.html { render :new }
format.json { render json: #item.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /items/1
# PATCH/PUT /items/1.json
def update
respond_to do |format|
if #item.update(item_params)
format.html { redirect_to #item, notice: 'Item was successfully updated.' }
format.json { render :show, status: :ok, location: #item }
else
format.html { render :edit }
format.json { render json: #item.errors, status: :unprocessable_entity }
end
end
end
# DELETE /items/1
# DELETE /items/1.json
def destroy
#item.destroy
respond_to do |format|
format.html { redirect_to items_url, notice: 'Item was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_item
#item = Item.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def item_params
params.require(:item).permit(:title, :description, :price, :available, :sort_shop, :sort_gallery, :item_type, :size)
end
end
form.html.erb
<%= form_for #item, html: { multipart: true } do |f| %>
<% if #item.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#item.errors.count, "error") %> prohibited this item from being saved:</h2>
<ul>
<% #item.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.label :price %><br>
<%= f.text_field :price %>
</div>
<div class="field">
<%= f.label :available %><br>
<%= f.check_box :available %>
</div>
<div class="field">
<%= f.label :sort_shop %><br>
<%= f.number_field :sort_shop %>
</div>
<div class="field">
<%= f.label :sort_gallery %><br>
<%= f.number_field :sort_gallery %>
</div>
<div class="field">
<%= f.label :item_type %><br>
<%= f.text_field :item_type %>
</div>
<div class="field">
<%= f.label :size %><br>
<%= f.text_field :size %>
</div>
<%= f.fields_for :item_images do |builder| %>
<% if builder.object.new_record? %>
<div class="field">
<%= builder.label :image, "Image File" %>
<%= builder.file_field :image %>
</div>
<% end %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Try this in strong parameters in Items controller
params.require(:item).permit(:title, :description, :price, :available, :sort_shop, :sort_gallery, :item_type, :size,item_images_attributes: [:image ])
than in ItemImage.rb add this line
belongs_to :item, optional: true,
and remove this line from Item.rb
:reject_if => lambda { |t| t['item_image'].nil? }
`
If you get any error please reply
I'm making a survey app, I've got 3 models: Survey, Question, and Answer. Survey has_many questions, Question has_many answers.
I've got two main errors:
My code is supposed to generate 4 answer fields for each question but only 1 answer field is generated.
When I press submit on my form, I get
unknown attribute 'answers' for Survey.
Extracted source (around line #33):
# POST /surveys.json
def create
#survey = Survey.new(survey_params)
respond_to do |format|
if #survey.save
I think the second problem is related to the answers model in some way but I'm not sure how. Here's my code:
surveys_controller:
class SurveysController < ApplicationController
before_action :set_survey, only: [:show, :edit, :update, :destroy]
# GET /surveys
# GET /surveys.json
def index
#surveys = Survey.all
end
# GET /surveys/1
# GET /surveys/1.json
def show
end
# GET /surveys/new
def new
#survey = Survey.new
3.times do
question = #survey.questions.build
4.times { question.answers.build }
end
end
# GET /surveys/1/edit
def edit
end
# POST /surveys
# POST /surveys.json
def create
#survey = Survey.new(survey_params)
respond_to do |format|
if #survey.save
format.html { redirect_to #survey, notice: 'Survey was successfully created.' }
format.json { render :show, status: :created, location: #survey }
else
format.html { render :new }
format.json { render json: #survey.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /surveys/1
# PATCH/PUT /surveys/1.json
def update
respond_to do |format|
if #survey.update(survey_params)
format.html { redirect_to #survey, notice: 'Survey was successfully updated.' }
format.json { render :show, status: :ok, location: #survey }
else
format.html { render :edit }
format.json { render json: #survey.errors, status: :unprocessable_entity }
end
end
end
# DELETE /surveys/1
# DELETE /surveys/1.json
def destroy
#survey.destroy
respond_to do |format|
format.html { redirect_to surveys_url, notice: 'Survey was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_survey
#survey = Survey.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def survey_params
params.require(:survey).permit!
end
end
surveys/_form.html.erb
<%= form_for(#survey) do |f| %>
<% if #survey.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#survey.errors.count, "error") %> prohibited this survey from being saved:</h2>
<ul>
<% #survey.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<%= f.fields_for :questions do |builder| %>
<p>
<%= builder.label :content, "Question" %><br />
<%= builder.text_area :content, :rows => 3 %>
</p>
<%= f.fields_for :answers do |builder| %>
<p>
<%= builder.label :content, "Answer" %>
<%= builder.text_field :content %>
</p>
<% end %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
surveys/show.html.erb
<p id="notice"><%= notice %></p>
<p>
<strong>Name:</strong>
<%= #survey.name %>
</p>
<ol>
<% #survey.questions.each do |question| %>
<li><%= question.content %>
<ul>
<% for answer in question.answers %>
<li><%= answer.content %></li>
<% end %>
</ul>
</li>
<% end %>
</ol>
<%= link_to 'Edit', edit_survey_path(#survey) %> |
<%= link_to 'Back', surveys_path %>
survey.rb:
class Survey < ActiveRecord::Base
has_many :questions, :dependent => :destroy
accepts_nested_attributes_for :questions, :reject_if => -> (a) {a[:content].blank? }, :allow_destroy => true
end
question.rb:
class Question < ActiveRecord::Base
belongs_to :survey
has_many :answers, :dependent => :destroy
accepts_nested_attributes_for :answers, :reject_if => -> (a) {a[:content].blank? }, :allow_destroy => true
end
answer.rb
class Answer < ActiveRecord::Base
belongs_to :question
end
Any help would be appreciated, I've been stuck on this for hours now!
I don't know if the errors could be related to the fields_for, what happens when you do:
<%= f.fields_for :questions do |question_attribute| %>
<p>
<%= question_attribute.label :content, "Question" %><br />
<%= question_attribute.text_area :content, :rows => 3 %>
</p>
<%= question_attribute.fields_for :answers do |answer_attribute| %>
<p>
<%= answer_attribute.label :content, "Answer" %>
<%= answer_attribute.text_field :content %>
</p>
<% end %>
<% end %>
Let me know what is the outcome of that.
i am using carrierwave for multiple file uploads.
My post model:
class Post < ActiveRecord::Base
has_many :post_attachments
accepts_nested_attributes_for :post_attachments
end
post_attachment.rb:
class PostAttachment < ActiveRecord::Base
mount_uploader :avatar, AvatarUploader
belongs_to :post
end
posts_controller.rb:
def show
#post_attachments = #post.post_attachments.all
end
def new
#post = Post.new
#post_attachment = #post.post_attachments.build
end
def create
#post = Post.new(post_params)
respond_to do |format|
if #post.save
params[:post_attachments]['avatar'].each do |a|
#post_attachment = #post.post_attachments.create!(:avatar => a, :post_id => #post.id)
end
format.html { redirect_to #post, notice: 'Post was successfully created.' }
else
format.html { render action: 'new' }
end
end
end
private
def post_params
params.require(:post).permit(:title, post_attachments_attributes: [:id, :post_id, :avatar])
end
and my posts/_form is:
<%= form_for(#post, :html => { :multipart => true }) do |f| %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<%= f.fields_for :post_attachments do |p| %>
<div class="field">
<%= p.label :avatar %><br>
<%= p.file_field :avatar, :multiple => true, name: "post_attachments[avatar][]" %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Actually the above code is referred from Rails 4 multiple image or file upload using carrierwave.
Initially i have done single file upload using http://richonrails.com/articles/allowing-file-uploads-with-carrierwave where I used to download the file. For this I used to write a method in my controller as:
def download
respond_to do |format|
format.html {
if params[:post_id].nil?
redirect_to :back
else
begin
post = Post.find(params[:post_id])
attachment_file = File.join('public', post.attachment.url)
if File.exists?(attachment_file)
send_file attachment_file, :disposition => 'attachment'
else
redirect_to :back
end
rescue Exception => error
redirect_to :back
end
end
}
end
end
and in posts/index.html.erb:
<% #posts.each do |post| %>
<tr>
<td align="center">
<%= link_to image_tag("download.png"), download_post_path(post.id) %>
</td>
</tr>
<% end %>
attachment_uploader.rb:
class AttachmentUploader < CarrierWave::Uploader::Base
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
How can I download multiple files for a post_id as a zip with my code?
I have a form for creating a project, the project can contain five photos.
In my _form.html.erb I can't access my upladed photos when I'm calling multipart => true but in my show they are accessible. If there are any existing photos, I want to show the existing photos in the edit.erb and a delete option. I can access the uploaded photos in the show.
Here is my _form.html.erb:
<%= form_for #project, :html => { :multipart => true } do |f| %>
<% if #project.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#project.errors.count, "error") %> prohibited this project from being saved:</h2>
<ul>
<% #project.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_area :description %>
</div>
<div class="newPaperclipFiles">
<%= f.fields_for :assets do |asset| %>
<% if asset.object.new_record? %>
<%= asset.file_field :photo %>
<% end %>
<% end %>
</div>
<div class="existingPaperclipFiles">
<% f.fields_for :assets do |asset| %>
<% unless asset.object.new_record? %>
<div>
<%=image_tag asset.photo.url(:small) %>
</div>
<%= asset.check_box :_destroy %>
<% end %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
The show.erb where I can access the photos:
<div class="existingPaperclipFiles">
<% #project.assets.each do |asset| %>
<div>
<%= image_tag asset.photo.url(:small) %>
</div>
<% end %>
</div>
<p>
<strong>Title:</strong>
<%= #project.title %>
</p>
<p>
<strong>Description:</strong>
<%= #project.description %>
</p>
<%= link_to 'Edit', edit_project_path(#project) %> |
<%= link_to 'Back', projects_path %>
My asset model:
class Asset < ActiveRecord::Base
require 'paperclip'
belongs_to :project, :foreign_key => "project_id"
attr_accessible :project_id, :photo
has_attached_file :photo, :styles => { :thumb => "600x600#", :medium => "300x300#", :small => "160x160#"}
end
My project model:
class Project < ActiveRecord::Base
has_permalink :title
default_scope :order => 'created_at desc'
attr_accessible :title, :description, :assets_attributes, :dependent => :destroy
validates_uniqueness_of :title
validates_presence_of :title
has_many :assets, :dependent => :destroy
accepts_nested_attributes_for :assets, :allow_destroy => true
end
EDIT:
Project contoller:
class ProjectsController < ApplicationController
before_filter :authenticate_admin!, :except => [:show]
before_action :set_project, only: [:show, :edit, :update, :destroy]
# GET /projects
# GET /projects.json
def index
#projects = Project.all
end
# GET /projects/1
# GET /projects/1.json
def show
end
# GET /projects/new
def new
#project = Project.new()
(5 - #project.assets.length).times { #project.assets.build }
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #project }
end
end
# GET /projects/1/edit
def edit
#project = Project.find_by_permalink(params[:id])
#assets = Project.includes(:assets).find_by_permalink(params[:id])
(5 - #project.assets.length).times { #project.assets.build }
end
# POST /projects
# POST /projects.json
def create
#project = Project.create(params[:project])
respond_to do |format|
if #project.save
format.html { redirect_to #project, notice: 'Project was successfully created.' }
format.json { render action: 'show', status: :created, location: #project }
else
format.html { render action: 'new' }
format.json { render json: #project.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /projects/1
# PATCH/PUT /projects/1.json
def update
#project = Project.find_by_permalink(params[:id])
respond_to do |format|
if #project.update(params[:project])
format.html { redirect_to #project, notice: 'Project was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #project.errors, status: :unprocessable_entity }
end
end
end
# DELETE /projects/1
# DELETE /projects/1.json
def destroy
#project = Project.find_by_permalink(params[:id])
#project.destroy
respond_to do |format|
format.html { redirect_to projects_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_project
#project = Project.find_by_permalink(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def project_params
params.require(:project).permit(:title, :description, :assets_attributes)
end
end
Not sure if this is a typo, but try adding the = here:
<% f.fields_for :assets do |asset| %>
<%= f.fields_for :assets do |asset| %>
I tried to submit a new post and I get the error
"Title can't be blank"
So I removed the validations in my model and after trying again and posting something, the post is just blank, no data is saved whatsoever.
I don't know what to do, I stuck on this one, help!
Update!
Here is the form
<% #post.tags.build %>
<%= form_for #post, :html => {:multipart => true } do |post_form| %>
<% if #post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#post.errors.count, "error") %> prohibited this post from being saved: </h2>
<ul>
<% #post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= post_form.file_field :photo %>
</div>
<div class="field">
<%= post_form.label :title %><br />
<%= post_form.text_field :title %>
</div>
<div class="field">
<%= post_form.label :url %><br />
<%= post_form.text_field :url %>
</div>
<div class="field">
<%= post_form.label :company %><br />
<%= post_form.text_field :company %>
</div>
<div class="field">
<%= post_form.label :language %><br />
<%= post_form.text_field :language %>
</div>
<div class="field">
<%= post_form.label :framework %><br />
<%= post_form.text_field :framework %>
</div>
<div class="field">
<%= post_form.label :details %><br />
<%= post_form.text_area :details %>
</div>
<h2>Tags</h2>
<%= render :partial => 'tags/form' ,
:locals => {:form => post_form } %>
<div class="actions">
<%= post_form.submit %>
</div>
<% end %>
here is the controller:
class PostsController < ApplicationController
http_basic_authenticate_with :name => "franklinexpress", :password => "osxuser8", :except => [:index, :show, :new, :edit]
#def search
# #posts = Post.search(params[:search])
# end
# GET /posts
# GET /posts.json
def index
#posts = Post.search(params[:search])
# #posts = Post.all
# respond_to do |format|
#format.html # index.html.erb
#format.json { render json: #posts }
#end
end
# GET /posts/1
# GET /posts/1.json
def show
#post = Post.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #post }
end
end
# GET /posts/new
# GET /posts/new.json
def new
#post = Post.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #post }
end
end
# GET /posts/1/edit
def edit
#post = Post.find(params[:id])
end
# POST /posts
# POST /posts.json
def create
#post = Post.new(params[:post])
respond_to do |format|
if #post.save
format.html { redirect_to #post, notice: 'Post was successfully created.' }
format.json { render json: #post, status: :created, location: #post }
else
format.html { render action: "new" }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# PUT /posts/1
# PUT /posts/1.json
def update
#post = Post.find(params[:id])
respond_to do |format|
if #post.update_attributes(params[:post])
format.html { redirect_to #post, notice: 'Post was successfully updated.' }
format.json { head :ok }
else
format.html { render action: "edit" }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
#post = Post.find(params[:id])
#post.destroy
respond_to do |format|
format.html { redirect_to posts_url }
format.json { head :ok }
end
end
end
in my model:
class Post < ActiveRecord::Base
validates :title, :presence => true
validates :url, :presence => true
validates :company, :presence => true
validates :language, :presence => true
validates_attachment_size :photo, :less_than => 4.megabytes
validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png']
has_many :comments, :dependent => :destroy
has_many :tags
attr_accessor :photo_file_name
attr_accessor :photo_content_type
attr_accessor :photo_file_size
attr_accessor :photo_updated_at
attr_accessible :photo
accepts_nested_attributes_for :tags, :allow_destroy => :true,
:reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
#paperclip-------------------------------
has_attached_file :photo,
:url => "/assests/images/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/images/:id/:style/:basename.:extension"
#:style => {:small => "150x200>"}
def self.search(search)
if search
where('title LIKE ?', "%#{search}%")
# find(:all, :conditions => ['title LIKE ?', "%#{search}%"])
else
all
end
end
end
and in new.html.erb:
<div id="header-wrap">
<%= image_tag("walLogotiny.png") %>
<div id="searchbartop">
<%= form_tag posts_path, :method => :get do%>
<%= text_field_tag :search, params[:search] ,"size" => 100 %>
<%= submit_tag "Search", :name => nil %>
<% end %>
</div>
</div>
<div id="container">
<h2>New Site Submission</h2>
<%= render 'form' %>
<%= link_to 'Back', posts_path %>
</div>
With this line in your model:
attr_accessible :photo
You make only the photo attribute mass-assignable. All other attributes, including the title, are dropped when you create a new post.
Try this:
attr_accessible :photo, :title
It will now accept the title, but not the other attributes.
edit: didn't see your own comment above, but you figured it out already.