I am really new to nested forms in Rails, so I am sure I probably left something out here. I am following a Paperclip tutorial where an article has many assets (paperclip attachments)
The part I am stuck at is creating multiple file upload fields with the article controller.
You will see in the form partial that I have added the asset model near the bottom:
<%= form_for(#article) do |f| %>
<% if #article.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#article.errors.count, "error") %> prohibited this article from being saved:</h2>
<ul>
<% #article.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 :content %><br>
<%= f.text_area :content %>
</div>
<%= f.fields_for :asset do |asset| %>
<%= asset.file_field :image %>
<% end %>
<div class="actions">
<%= f.submit %>
<% end %>
And in the article controller, I added what I thought would build that portion of the form 5 times:
def new
#article = Article.new
5.times {#article.assets.build}
end
For good measure, here is the article and asset models:
article
class Article < ActiveRecord::Base
has_many :assets
end
assets
class Asset < ActiveRecord::Base
belongs_to :article
has_attached_file :image,
:style => {
:thumb => '150x150#',
:medium => '300x300>',
:large => '600x600>'
}
end
What am I missing?
In order to get multiple fields for :assets generated, you need to add accepts_nested_attributes_for :assets in your Article model.
# article.rb
class Article < ActiveRecord::Base
has_many :assets
accepts_nested_attributes_for :assets
end
Then in your view:
<%= f.fields_for :assets do |asset| %>
<%= asset.file_field :image %>
<% end %>
I think you need to specify the nested fields as plural (same as the association):
<%= f.fields_for :assets do |asset| %>
<%= asset.file_field :image %>
<% end %>
Related
A similar question has already been asked, but the answer is only for Rails 3, so I am taking the liberty of asking a new question.
I have a Rails 4 app, with the following models:
class User < ActiveRecord::Base
has_many :administrations
has_many :calendars, through: :administrations
end
class Calendar < ActiveRecord::Base
has_many :administrations
has_many :users, through: :administrations
has_many: :posts
end
class Administration < ActiveRecord::Base
belongs_to :user
belongs_to :calendar
end
class Post < ActiveRecord::Base
belongs_to :calendar
end
The Post model has the following attributes:
references :calendar, index: true, foreign_key: true
date :date
time :time
string :subject
string :format
text :copy
attachment :image
and the attachment was setup as follows with Paperclip in Post model:
has_attached_file :image, styles: { small: "64x64", med: "100x100", large: "200x200" }
validates_attachment :image, :content_type => { :content_type => "image/png" },
:size => { :in => 0..3000.kilobytes }
Adding an image to a Post is working fine, through the following form:
<%= form_for [#calendar, #calendar.posts.build] 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">
<p>
<%= f.label :date %><br>
<%= f.date_select :date %>
</p>
<p>
<%= f.label :time %><br>
<%= f.time_select :time %>
</p>
<p>
<%= f.label :subject %><br>
<%= f.text_field :subject %>
</p>
<p>
<%= f.label :format %><br>
<%= f.text_field :format %>
</p>
<p>
<%= f.label :copy %><br>
<%= f.text_area :copy %>
</p>
<p>
<%= f.label :image %><br>
<%= f.file_field :image %>
</p>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Viewing the image attached to a Post is also working well, thanks to this show view:
<h2>Post from <%= #post.date.strftime("%A, %d") %></h2>
<div>
<p><strong>Date</strong></p>
<%= #post.date.strftime("%A, %d") %>
</div>
<div>
<p><strong>Time</strong></p>
<%= #post.time.strftime("%I:%M %p") %>
</div>
<div>
<p><strong>Subject</strong></p>
<%= #post.subject %>
</div>
<div>
<p><strong>Format</strong></p>
<%= #post.format %>
</div>
<div>
<p><strong>Copy</strong></p>
<%= #post.copy %>
</div>
<div>
<p><strong>Image</strong></p>
<%= image_tag #post.image.url(:med) %>
</div>
However, when I go to the edit view, for a post where I have previously uploaded an image, I only see the field that allows me to add an image and a message saying "no file chosen".
The edit view uses (for now) the same form as the new view, as shown above.
How can I achieve the following:
Have the uploaded image appear in the edit page.
Allow users to delete this image.
Allow users to replace this image with a new one.
–––––
UPDATE: I found a way to solve item #1 above, by replacing
<p>
<%= f.label :image %><br>
<%= f.file_field :image %>
</p>
with
<p>
<%= f.label :image %><br>
<% if #post.image.exists? %>
<%= image_tag #post.image.url(:med) %>
<% else %>
<%= f.file_field :image %>
<% end %>
</p>
in my Post edit view.
I am still working on items #2 & #3 and could definitely use some help with these.
–––––
I am happy to share more code if necessary.
Any idea?
For #2 - Allow user to delete image, you might find this SO Issue helpful
OR, as per Paperclip's documentation you could simply create a dummy checkbox and, in the update method of your controller, look if the checkbox has been ticked and delete the attachment as mentioned in the documentation.
For #3 - Allow users to replace this image with a new one
Simply rework your code as follow:
<p>
<%= f.label :image %><br>
<% if #post.image.exists? %>
<%= image_tag #post.image.url(:med) %>
<% end %>
<%= f.file_field :image %>
</p>
This way, the file_field is always present (allowing user to replace or add their image)
I have this data inside my foods and drinks table:
Foods table
ID | Name
------------
1 | Rojak
2 | Lemang
Drinks table
ID | Name
------------
1 | Barli
2 | Sirap
My model relationship is:
class Survey < ActiveRecord::Base
has_many :questions
accepts_nested_attributes_for :questions, allow_destroy: true
end
class Question < ActiveRecord::Base
belongs_to :survey
has_many :answers
accepts_nested_attributes_for :answers, allow_destroy: true
end
class Answer < ActiveRecord::Base
belongs_to :question
has_many :foods
has_many :drinks
accepts_nested_attributes_for :foods, :drinks
end
class Food < ActiveRecord::Base
belongs_to :answer
end
class Drink < ActiveRecord::Base
belongs_to :answer
end
And this is my _form.html.erb file inside app/views/surveys:
<%= form_for #survey do |f| %>
<% if #survey.errors.any? %>
<div class="error_messages">
<h2><%= pluralize(#survey.errors.count, "error") %> prohibited this survey from being saved:</h2>
<ul>
<% #survey.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<br>
<%= f.fields_for :questions do |f| %>
<fieldset>
<%= f.label :content, "Question" %><br />
<%= f.text_area :content %><br />
<% Food.all.each do |fd| %>
<fieldset>
<%= f.fields_for :answers do |f| %>
<%= f.text_field :name, :value => fd.name %>
<%= f.number_field :quantity %>
<% end %> <br>
</fieldset>
<% end %>
</fieldset>
<br>
<% end %>
<br>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
What I'm trying to do is:
List all foods name and drinks name inside foods and drinks fieldset
Give each of the foods and drinks a quantity (this quantity column is inside answers table)
Save and update it
But I got no luck. When I'm try to load the edit view (the form), I managed to list all of the foods name. But, I've no idea how to list all of the drinks name in its own fieldset.
I try to make it like this but it didn't work:
<% if :content == "Foods" %>
<%= #loads foods %>
<% else %>
<%= #loads drinks %>
<% end %>
And, when I try to save the foods/drinks name and its quantity, it didn't work too.
Demo:
How to fix this problem?
Note:
This is update method inside surveys_controller:
def update
if #survey.update(survey_params)
redirect_to #survey, notice: "Successfully updated survey."
else
render :edit
end
end
...
def survey_params
params.require(:survey).permit(:name, questions_attributes: [:id, :_destroy, :content, answers_attributes: [:id, :_destroy, :content, :quantity]])
end
Update:
This is my show.html.erb
<h1><%= #survey.name %></h1>
<ul class="questions">
<% #survey.questions.each do |question| %>
<li>
<%= question.content %>
<ol class="answers">
<% question.answers.each do |answer| %>
<li><%= answer.content %> (<%= answer.quantity %>)</li>
<% end %>
</ol>
</li> <br>
<% end %>
</ul>
<%= link_to 'Edit', edit_survey_path(#survey) %> |
<%= link_to 'Back to Surveys', surveys_path %>
You might be seeing an issue with variable scope. Each time you create a nested form with f.fields_for you pass in the variable f, even though f is the variable used in the parent.
Try calling it something different with questions and answers, such as:
<%= f.fields_for :questions do |fq| %>
<fieldset>
<%= fq.label :content, "Question" %><br />
<%= fq.text_area :content %><br />
<% Food.all.each do |fd| %>
<fieldset>
<%= fq.fields_for :answers do |fa| %>
<%= fa.text_field :name, :value => fd.name %>
<%= fa.number_field :quantity %>
<% end %> <br>
</fieldset>
<% end %>
</fieldset>
<br>
<% end %>
I'm working on a nested form where a user when creating a Screen should also attached an image (a separate model called Screenshot). I'm trying to validate the presence of the attachment before saving the new Screen to the db. I tried to validate the presence of the attachment in the Screenshot model, but that only precent Screenshot to be saved, while it still creates a Screen.
Here's my models:
class Screen < ActiveRecord::Base
belongs_to :project
has_many :screenshots
validates :name, presence: true
accepts_nested_attributes_for :screenshots
validates_presence_of :screenshots
end
class Screenshot < ActiveRecord::Base
belongs_to :screen
has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }
end
here's my controller:
def create
#screen = Screen.create(screen_params)
if #screen.save
flash[:notice] = "A new screen has been added to this project"
redirect_to [#screen.project]
else
render :action => 'new'
end
end
And lastly here's my form:
<%= form_for ([#project, #screen]), :html => { :multipart => true } do |f| %>
<% if #screen.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#screen.errors.count, "error") %> prohibited this screen from being saved:</h2>
<ul>
<% #screen.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.hidden_field :project_id %>
</div>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_field :description %>
</div>
<%= f.fields_for :screenshots, #screen.screenshots.build do |s| %>
<%= s.hidden_field :screen_id, :value => #screen.id %>
<%= s.hidden_field :version, :value => "1" %>
<%= s.label :image %><br>
<%= s.file_field :image %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Any help to this noob is greatly appreciated.
Several things for you:
Form
You shouldn't build new objects in the form itself. That needs to happen in the controller's new function
<%= f.fields_for :screenshots do |s| %>
<%= s.hidden_field :screen_id, :value => #screen.id %>
<%= s.hidden_field :version, :value => "1" %>
<%= s.label :image %><br>
<%= s.file_field :image %>
<% end %>
You can declare them like this:
def new
#screen = Screen.new
#screen.screenshots.build
#any more build declarations go here
end
Verification
I believe you should put the verification into the screnshots model, as the nested parameters are passed directly to the corresponding model. I'm not sure on that though.
In the screenshots model, I would put the validates_presence_of :screenshots, with the name as your Paperclip attachment's name
Again I need your help. Now I need to understand how I can delete with Carrierwave uploaded files (in my case - images).
models/attachment.rb :
class Attachment < ActiveRecord::Base
belongs_to :attachable, :polymorphic => true
attr_accessible :file, :file
mount_uploader :file, FileUploader
end
models/post.rb :
class Post < ActiveRecord::Base
attr_accessible :content, :title, :attachments_attributes, :_destroy
has_many :attachments, :as => :attachable
accepts_nested_attributes_for :attachments
end
*views/posts/_form.html.erb :*
<%= nested_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 |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div id="field">
<%= f.label :Nosaukums %>:<br /><br />
<%= f.text_field :title %><br /><br />
</div>
<div id="field">
<%= f.label :Raksts %>:<br /><br />
<%= f.text_area :content %><br /><br />
</div>
<%= f.fields_for :attachments do |attachment| %>
<% if attachment.object.new_record? %>
<%= attachment.file_field :file %>
<% else %>
<%= image_tag(attachment.object.file.url) %>
<%= f.check_box :_destroy %>
<% end %>
<% end %>
<%= f.submit "Publicēt", :id => "button-link" %>
<% end %>
When I am trying to delete previous uploaded file I have this error:
unknown attribute: _destroy
Maybe there is problem because I have multiple file uploads not only one.
None of this worked for me, but after digging I came across this post that really helped. Basically...
Form (where f is your form objects):
<%= f.check_box :remove_image %>
Then, if you check the box and submit the form you'll get the following error:
Can't mass-assign protected attributes: remove_image
Which is easily solved by simply adding remove_image to your attr_accessible list on the model. In the end it'll look something like:
class Background < ActiveRecord::Base
attr_accessible :image, :remove_image
belongs_to :user
mount_uploader :image, BackgroundUploader
end
In my case it's a background image that belongs to the user. Hope this helps :)
According to the docs, the checkbox should be called remove_file.
You're calling the method on the wrong model. Your file mount is on the Attachment.
The error is telling you what is wrong.
undefined method 'remove_file' for #<Post:0x471a320
The key point of the error is the method is being called on the Post model when it needs to be called on the Attachment model.
Maybe try scoping the input for the checkbox to the correct model.
<%= attachment.check_box :remove_file %>
It should be
<%= attachment.check_box :_destroy%>
It works for me
I have the 3 Models, that says a Voter has many votes and i'm trying to get the voter to vote on the same form as when the voting object is created but the field in my form isn't showing.
Here are my models:
class Voter < ActiveRecord::Base
attr_accessible :email_address, :verification_code, :verified, :votes_attributes
has_many :votes, :class_name => "Vote"
accepts_nested_attributes_for :votes
end
class Vote < ActiveRecord::Base
belongs_to :entry
belongs_to :voter
attr_accessible :entry, :voter, :voter_id
end
And in my form I have:
<%= form_for(#voter) do |f| %>
<% if #voter.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#voter.errors.count, "error") %> prohibited this voter from being saved:</h2>
<ul>
<% #voter.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :email_address %><br />
<%= f.text_field :email_address %>
</div>
<div class="field">
<%= f.label :verification_code %><br />
<%= f.text_field :verification_code %>
</div>
<div class="field">
<%= f.label :verified %><br />
<%= f.check_box :verified %>
</div>
<div class="field">
<% f.fields_for :votes do |builder| %>
<fieldset>
<%= builder.label :votes, "Entry" %>
<%= collection_select(:entry, :entry_id, Entry.all, :id, :email_address, :prompt => 'Please select an Entry') %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
But the votes field isn't showing. And I can't understand why.
In rails 3 you need to use:
<%= f.fields_for :votes do |builder| %>
This will fix the problem.