I am trying to get Paperclip to accept an mp3 file. I was able to get this to work in a Rails 2 app, but am having difficulty in Rails 3. I can get the file to show up in my assets directory, but I continue to get the missing.png listed instead of the appropriate file in the show action.
Here is my model code...
has_attached_file :sermonfile,
:url => "/assets/:class/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/:class/:style/:id/:basename.:extension"
attr_accessor :sermonfile_file_name attr_accessor
:sermonfile_content_type
attr_accessor :sermonfile_file_size
attr_accessor :sermonfile_updated_at
Here is the form view...
<%= form_for #sermon, :html =>
{:multipart => true} do |f| %> <% if
#sermon.errors.any? %>
<%= pluralize(#sermon.errors.count,
"error") %> prohibited this sermon
from being saved:
<ul>
<% #sermon.errors.full_messages.each do
|msg| %>
<%= msg %>
<% end %>
<% end %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :permalink %>
<%= f.text_field :permalink %>
<%= f.label :speaker %>
<%= f.text_field :speaker %>
<%= f.label :date %>
<%= f.date_select :date %>
<%= f.label :series %>
<%= f.text_field :series %>
<%= f.label :book %>
<%= f.text_field :book %>
<%= f.label :passage %>
<%= f.text_field :passage %>
<%= f.label :notes %>
<%= f.text_area :notes, :class => "mceEditor" %> <%= f.file_field
:sermonfile %>
<%= f.submit %> <% end %>
This is what I am using to render the file in the show view...
<%= link_to #sermon.sermonfile.url %>
Any assistance is greatly appreciated!
Your :url & :path have their :id & :style mixed up. They should be the same: :id/:style
has_attached_file :sermonfile,
:url => "/assets/:class/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/:class/:id/:style/:basename.:extension"
Related
I have a nested form that saves information to three different models. One section of the form uses checkboxes and is supposed to save values 1-5. However, even when the boxes are checked the form returns value 0. I have tried several different variations of code for setting the checked value. Any help would be much appreciated. A section of the form code is below:
<%= form_for #newinstructor do |f|%>
<%= f.text_field :first_name %>
<%= f.text_field :last_name %>
<%= f.fields_for :through_ats do |tag_field| %>
<%= label_tag("What categories does your activity fit into?") %><br>
<%= label_tag(:tag, "Cooking") %>
<%= tag_field.check_box(:tag_id, :value => 1) %>
<%= label_tag(:tag, "Art") %>
<%= tag_field.check_box(:tag_id, :value => 2) %>
<%= label_tag(:tag, "Music") %>
<%= tag_field.check_box(:tag_id, :value => 3) %>
<%= label_tag(:tag, "Outdoors") %>
<%= tag_field.check_box(:tag_id, :value => 4) %>
<%= label_tag(:tag, "Food") %>
<%= tag_field.check_box(:tag_id, :value => 5) %>
<% end %>
<%= f.submit %>
<% end %>
Did you permit tag_id in params?
params.require(:instructor).permit(:name,
through_ats_attributes: [:id, :tag_id, :_destroy]
)
To fix the problem I had, I had to uniquely define each of the symbols in tag_field.checkbox, then require/permit them individually in params
so in the form i put:
<%= label_tag("Please check off the categories your activity fits into.") %><br>
<%= label_tag(:tag, "Cooking") %>
<%= tag_field.check_box(:cooking) %>
<%= label_tag(:tag, "Art") %>
<%= tag_field.check_box(:art) %>
instead of :
<%= label_tag("What categories does your activity fit into?") %><br>
<%= label_tag(:tag, "Cooking") %>
<%= tag_field.check_box(:tag_id, :value => 1) %>
<%= label_tag(:tag, "Art") %>
<%= tag_field.check_box(:tag_id, :value => 2) %>
and in the controller:
params.require(:instructor).permit(through_ats: [:cooking, :art, :music, :outdoors, :food])
instead of:
params.require(:instructor).permit(through_ats: [:id, :tag_id])
I guess I'm a little confused on how fields_for works. I have an action with this:
3.times { #submitted_quiz.submitted_answers.build }
if I write a form_for like this in the associated view:
<%= form_for(#submitted_quiz) do |f| %>
<%= f.hidden_field :quiz_id, :value => #quiz.id %>
<%= f.hidden_field :name, :value => #quiz.name %>
<%= f.fields_for (:submitted_answers) do |ff| %>
<%= ff.label :content, "Answer" %><br />
<%= ff.text_area :content, :rows => 3 %>
<% end %>
<%= f.submit %>
<% end %>
how does fields_for know to run three times?
Rails will simply check how many submitted_answers your submitted_quiz has and generate appropriate number of fields ( one for each answer obviously ).
I am desperately trying to put a multipart to my form in Ruby but it won't show up. I looked up online everywhere but whatever I try it doesn't show. Even simple IDs or classes won't work...
Is there any dependency I am not aware of?
<%= form_for #listing, :html => {:id => "account_form", :multipart => true } do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :title %>
<%= f.text_field :title, class: 'form-control' %>
<%= f.label :highlights %>
<%= f.text_area :highlights, class: 'form-control' %>
<%= f.label :location %>
<%= f.text_area :location, class: 'form-control' %>
<%= f.label :catering %>
<%= f.text_area :catering, class: 'form-control' %>
<%= f.label :travel %>
<%= f.text_area :travel, class: 'form-control' %>
<%= f.label :dates %>
<%= f.text_area :dates, class: 'form-control' %>
<%= f.label :price %>
<%= f.text_field :price, class: 'form-control' %>
<%= f.label :category %>
<%= f.select :category, options_from_collection_for_select(Category.all, :id, :name), :include_blank => true %>
<%= f.label :country %>
<%= f.text_field :country, class: 'form-control' %>
<%= f.label :url %>
<%= f.text_field :url, class: 'form-control' %>
<%= f.label :photo %>
<%= f.file_field :photo %>
<%= f.submit "Post", class: "btn btn-primary" %>
<% end %>
which will result in the following HTML
<form class="new_listing" id="new_listing" action="/listings" accept-charset="UTF-8" method="post">
Please help!
It should change to multipart by itself when you add f.file_field call:
Using this method inside a form_for block will set the enclosing form’s encoding to multipart/form-data.
As was found out in comments, error was due to using #listing inside of partial (instead of local variable).
If you have _listing_form.html.erb partial, you should pass the local variables manually:
<%= render partial: 'shared/listing_form', locals: {listing: #listing} %>
I have a nested simple form to edit a user. The user has a profile he/she can update, and a new record is written to the profile table.
simple_fields_for shows all profile records of a user due to the relationship user 1 to many profile records. However, I would like to only show the newest profile record in the form! How can I accomplish that?
<%= simple_form_for(#user) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :lang %>
<%= f.input :firstname %>
<%= f.input :lastname %>
<%= f.input :email %>
<%= f.input :born %>
<%= f.input :gender %>
<%= f.simple_fields_for :profile do |p| %> # the magic needed here
<%= p.input :postal_code %>
<%= p.input :core %>
<%= p.input :daytime %>
<%= p.input :style %>
<% end %>
<% if #is_new %>
<%= f.simple_fields_for :status do |s| %>
<%= s.input :entered, as: :hidden, input_html: { value: Time.current } %>
<% end %>
<% end %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
Little late, but accepted answer is not 100% accurate. (Would just make this a comment, but no rep.) I would do this like so:
Assuming you have
class User < ActiveRecord::Base
has_many :profiles
...
def latest_profile
profiles.order(...) # query to get whatever record it is you want
end
end
your simple_fields_for can just be
<%= f.simple_fields_for :profiles, f.object.latest_profile do |p| %>
Note the pluralization of :profiles, and that we don't need to clutter the controller with an additional instance variable because you can access the object in the form. (Using the singular will work I believe, but you will not get params with a key in the form of :profiles_attributes, meaning even more code to account for unique params.)
The edit action:
class user > ApplicationController
...
def edit
#profile = #user.profile.order("saved DESC").first
end
...
end
The working form:
<%= simple_form_for(#user) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :lang %>
<%= f.input :firstname %>
<%= f.input :lastname %>
<%= f.input :email %>
<%= f.input :born %>
<%= f.input :gender %>
<%= f.simple_fields_for :profile, #profile do |p| %> # the magic needed here
<%= p.input :postal_code %>
<%= p.input :core %>
<%= p.input :daytime %>
<%= p.input :style %>
<% end %>
<% if #is_new %>
<%= f.simple_fields_for :status do |s| %>
<%= s.input :entered, as: :hidden, input_html: { value: Time.current } %>
<% end %>
<% end %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
I feel like this should be really really simple, but I'm completely stuck!
In a form I might have a field like:
<%= f.text_field :name, :class => "text" %>
On edit, this pulls back in the value submitted on create, that's fine. But I want to prevent certain fields from being edited. I know I can disable the field or hide it, but I'd like to be able to pull in the values to display and use in other ways. How do I access them?
In this case I've tried things like:
<%= f.track.name %>
<%= track.name %>
<%= #track.name %>
But none of the above work!
Any ideas folks?
EDIT: (I'm using nested forms for this)
<%= form_for(#release, :html => { :multipart => true }) do |f| %>
<h3>Upload Tracks for <%= #release.title %></h3>
<%= f.fields_for :tracks do |builder| %>
<%= render 'upload_track_fields', :f => builder %>
<% end %>
<%= f.submit "Upload Tracks", :class => "submit" %>
<% end %>
And the upload_track_fields that are rendered:
<%= f.text_field :position, :class => "text" %>
<%= f.text_field :name, :class => "text" %>
<%= f.text_field :isrc, :class => "text" %>
<%= f.text_field :version, :class => "text" %>
<%= f.file_field :track, :class => "text" %>
<%= f.hidden_field :primary_genre_id %>
<%= f.hidden_field :secondary_genre_id %>
<%= f.hidden_field :alt_primary_genre %>
<%= f.hidden_field :alt_secondary_genre %>
<%= f.hidden_field :asset_tier %>
<%= f.hidden_field :preview_start %>
<%= f.hidden_field :parental_advisory %>
<%= f.hidden_field :available_separately %>
<%= f.hidden_field :_destroy %>
I've hidden most of the fields to prevent editing, but still need to see some of the fields so they're left as text fields. I tried to disable them, but that stops any changes (specifically the file upload) working.
In short, i'd prefer to display most of the above as text rather than form fields.
In the main form:
<% index = 0 %>
<% f.fields_for :tracks do |builder| %>
<%= #release.tracks[index].name %>
<%= render 'upload_track_fields', :f => builder %>
<% index += 1 %>
<% end %>
In the nested form:
<%= f.text_field :position, :class => "text" %>
# Notice that there's no "name" attribute
<%= f.text_field :isrc, :class => "text" %>
<%= f.text_field :version, :class => "text" %>
<%= f.file_field :track, :class => "text" %>
What I did in the first snippet is dirty, but I never used fields_for so I don't know how to get the actual index. I looked in Google, but I didn't find a solution so far.
I can't try it right now, I'll do it when I'll be home.
I suggest using this while finding a way to get the index.
Good luck!
As those who have commented said, I'd assume the <%= #track.name %> should work, if you have #track = #release.track (for instance) in your edit method in the controller.
Instead of keeping track of the index you can access the associated objects through builder, it's actually a track
<% f.fields_for :tracks do |builder| %>
<%= builder.object.name %>
<%= render 'upload_track_fields', :f => builder %>
<% end %>