I used paper clip in my web application, I use this to make a new product:
<% semantic_form_for #product do |f| %>
<% f.inputs do %>
<%= f.input :title %>
<%= f.input :price %>
<%= f.file_field :photo %>
<%= f.input :category , :include_blank => false %>
<% end %>
<%= f.buttons %>
<% end %>
And this to show product:
<% semantic_form_for #product do |f| %>
<%= image_tag #product.photo.url%>
<% f.inputs do %>
<%= f.input :title %>
<%= f.input :price %>
<%= f.file_field :photo %>
<%= f.input :category , :include_blank => false %>
<% end %>
<%= f.buttons %>
<% end %>
And his is my product.rb:
class Product < ActiveRecord::Base
validates_presence_of :title, :price
validates_numericality_of :price
validates_uniqueness_of :title
has_attached_file :photo
attr_accessible :name, :category_id, :price, :title, :photo
belongs_to :category
has_many :order_items
end
But after I upload the photo, it show my image path like this :
http://localhost:3000/photos/original/missing.png
It seems that it can't upload the photo with this error:
No route matches "/photos/original/missing.png" with
{:method=>:get}
I am not sure what semantic_form_for is, but with regular form_for, you have to explicitly say it's a multipart form.
<% form_for(#thing, :html => {:multipart => true}) do |f| %>
....
If you don't do this, the contents of your file upload fields won't be transmitted to the server. It's a HTML thing ;)
I think you paste the image into public/images folder in your rails application.And give the path like in your browser "/images/missing.png".
And your index will be
Related
I have the below form which works absolute fine but when submitted the :event field returns an ID in the mailer, any ideas how to prevent this?
Form
<%= simple_form_for #sponsorship_inquiry, :method => :post do |f| %>
<%= f.input :spam, as: :hidden %>
<%= f.input :name %>
<%= f.input :phone %>
<%= f.input :email %>
<%= f.input :job_title %>
<%= f.input :company %>
<%= f.input :event, :collection => Event.where(:end_date.gt => Date.today, :is_live => 'true') %>
<%= f.input :message, as: :text, :input_html => { :cols => 5, :rows => 6 } %>
<%= f.button :submit %>
<% end %>
Mailer
Name: <%= #sponsorship_inquiry.name %>
Phone: <%= #sponsorship_inquiry.name %>
E-Mail: <%= #sponsorship_inquiry.email %>
Job Title: <%= #sponsorship_inquiry.job_title %>
Company: <%= #sponsorship_inquiry.company %>
Event: <%= #sponsorship_inquiry.event %>
Message: <%= #sponsorship_inquiry.message %>
Controller
def new
#sponsorship_inquiry = SponsorshipInquiry.new
end
def create
# Hidden field for bots/spiders
redirect_to new_inquiry_path and return if params[:spam].present?
#sponsorship_inquiry = SponsorshipInquiry.new(params[:sponsorship_inquiry])
if #sponsorship_inquiry.valid?
SponsorshipInquiryMailer.admin(#sponsorship_inquiry).deliver
redirect_to sponsorship_inquiries_path
else
render :new
end
end
Need your SponsorshipInquiry model.
If you have
class SponsorshipInquiry < ActiveRecord::Base
belongs_to :event
end
try send <%= #sponsorship_inquiry.event.name %> or whatever )
Or you need to parse needed value from the form if "event" is only field not associated with Event model.
IMHO
If your question is "can I modify the form so that I automatically (magically?) get an object as a param?", the answer is definetly no.
What you have to do is to search the event object in the database based on the received id.
I have the next code in a partial _form of my view:
<%= f.label :logo %><br />
<% if f.object.new_record? %>
<%= f.file_field :logo %>
<% elsif %>
<%= link_to image_tag(f.object.logo.url(:thumb)), f.object.logo.url(:original) %>
<%= f.check_box %>
<% end %>
Im try to delete the object.logo if i select the checkbox and press the edit button. Im do it with Nested items, but this is a simple paperclip implementations.
any help is welcome, thank you.
You can do domething like this:
In the model:
accepts_nested_attributes_for :logos, :allow_destroy => true
In the form
if !f.object.logo_file_name.blank?
f.input :_destroy, :as => :boolean, :label => "Delete?"
end
Hope this help
Today i get the solution to this issue:
In the brand model:
attr_accessible :description, :title, :logo, :delete_logo
#delete existing logo from edit view with checkbox.
attr_accessor :delete_logo
before_validation { logo.clear if delete_logo == '1' }
In the brand view _form:
<% if #brand.logo? %>
<%= link_to image_tag(f.object.logo.url(:thumb)), f.object.logo.url(:original) %>
<%= f.check_box(:delete_logo) %>
<% else %>
<%= f.file_field :logo %>
<% end %>
Works for me.
I've got this tripbuilder which i want to assign categories to. So I've set up the models as where a trip can have any(or more) categories that are in the category table in my database. However; i have no idea how i can set up the form allowing a user to select categories via checkbox. Since fields_for doesn't sound like a solid way to go in this case (Because i want to see all the categories with a checkbox and select as many categories as i want). Can anyone help me out?
I've tried this form:
<%= form_for #trip, :html => {:multipart => true} do |a| %>
<%= a.label :title, "Routetitel" %>
<%= a.text_field :title %>
<%= a.label :description, "Omschrijving" %>
<%= a.text_area :description %>
<%= a.fields_for :categories do |cat| %>
<%= cat.check_box :name %>
<% end %>
<%= a.submit 'Verstuur' %>
<% end %>
At first, you need to setup the relationship between trip and category like this:
class Trip < ActiveRecord::Base
has_and_belongs_to_many :categories
end
Then you can build the form like this:
<%= form_for #trip, :html => {:multipart => true} do |a| %>
<%= a.label :title, "Routetitel" %>
<%= a.text_field :title %>
<%= a.label :description, "Omschrijving" %>
<%= a.text_area :description %>
<% Category.all.each do |cat| %>
<%= check_box_tag "trip[category_ids][]", cat.id, #trip.catergory_ids.include?(cat.id)
<% end %>
<%= a.submit 'Verstuur' %>
<% end %>
Yes, it can be done by using select tag and multiple attribute of select tag.
<% = a.select :categories, Category.all.collect {|c| [c.name, c.id]}, :include_blank => true', :multiple => "multiple" %>
Please Modify your fields_for as described below and check !!!!
<%= a.fields_for "categories[]" do |cat| %>
<%= cat.check_box :name %>
<% end %>
I am working on a dynamically nested form using the cocoon gem. I have two models
class CrossTable < ActiveRecord::Base
attr_accessible :title, :table_name, :database, :folder_label_id, :foreign_fields_attributes
belongs_to :folder_label
has_many :foreign_fields
accepts_nested_attributes_for :foreign_fields
validates :title, :table_name, :database, :folder_label_id, presence: true
end
class ForeignField < ActiveRecord::Base
attr_accessible :cross_table_id, :column_name, :description
belongs_to :cross_table
has_many :filter_sets
end
I have cocoon and jquery-rails in the gemfile
I added //=require cocoon to the application.js file
And here is my form partial
<%= simple_form_for #table do |f| %>
<%= f.input :title %>
<%= f.input :folder_label_id, :collection => #folders, :label_method => :title, :value_method => :id %>
<br><br>
<%= f.input :table_name %>
<%= f.input :database %>
<%= f.simple_fields_for :foreign_fields do |fields| %>
<%= render 'foreign_field_fields', :f => fields %>
<div id='links'>
<%= link_to_add_association 'Add Field', f, :foreign_fields %>
</div>
<% end %>
<%= f.button :submit %>
<% end %>
#table is an instance of the cross table model. Nothing in the foreign_field_fields partial shows up and link_to_add_association does nothing, and I get no errors. How can I start debugging this? Does anyone spot an error?
You wrote the link_to_add_association inside the simple_fields_for, which will loop over all :foreign_fields and execute the given block. So if there are no foreign-fields yet, the link_to_add_association is never shown.
You should write your view as follows (as documented):
<%= simple_form_for #table do |f| %>
<%= f.input :title %>
<%= f.input :folder_label_id, :collection => #folders, :label_method => :title, :value_method => :id %>
<br><br>
<%= f.input :table_name %>
<%= f.input :database %>
<%= f.simple_fields_for :foreign_fields do |fields| %>
<%= render 'foreign_field_fields', :f => fields %>
<% end %>
<div id='links'>
<%= link_to_add_association 'Add Field', f, :foreign_fields %>
</div>
<%= f.button :submit %>
<% end %>
Hope this helps.
Having problems saving the user_id to the database table Documents. Right now, it's not sending anything associated with user_id for documents when I check the rails server log in POST.
projects_controller.rb
def new_step_3
project = Project.new
#project.documents.build
end
new_step_3.html.erb
<%= form_for #project, :html => {:multipart => true} do |f| %>
<%= f.text_field :title %>
<%= f.text_field :description %>
<%= f.fields_for :documents do |builder| %>
<%= builder.file_field :title %>
<% end %>
<%= f.text_field :skills %>
<%= f.submit 'Post Project' %>
<% end %>
Did u include line accepts_nested_attributes_for :documents in your project model?