I use this code to upload photos, I find that it is not successfully upload photo, but other content can be updated.
<% semantic_remote_form_for(#product, :html => {:multipart => true}) do |f| %>
<% f.inputs do %>
<%= f.input :title, :label => "Name" %>
<%= f.input :category , :include_blank => false , :label => "Category" %>
<%= f.input :price, :label => "Price" %>
<%= f.input :photo, :label => "Photo" %>
<% end %>
<%= f.buttons %>
<% end %>
The code from server log:
DEPRECATION WARNING: Disabling
sessions for a single controller has
been deprecated. Sessions are now lazy
loaded. So if you don't access them,
consider them off. You can still
modify the session cookie options with
request.session_options.. (called from
/onlineStore/app/controllers/application_controller.rb:6)
Processing ProductsController#update
(for ::1 at 2010-01-23 22:03:54) [PUT]
Parameters: {"commit"=>"Save Product",
"authenticity_token"=>"vOvxOPYYE1wRGDYTEH5ciHrNJXUpGTJku3etIpCmf1c=",
"id"=>"33",
"product"=>{"price"=>"874",
"title"=>"Other products",
"category_id"=>"142"}, "_"=>""}
I'm surprised it worked at all, since Javascript is not supposed to be able to upload files via AJAX. Was the new switch the fact that you made it a remote form for the product? Since even though it's a gem helping with the semantic form building, even it shouldn't be able to do file uploads in that fashion.
Related
Can you please tell me if there is a way using simple_form with Rails, when you have "f.associations" present in a view, to disable the selected drop down menu in the form but still use the prefilled value of the field?
I have tried with option ":disabled => true" but it disables the whole text field and a I need a value to be present here. When I submit the form I receive an error that a value should be present.
I have tried with option ":as => :hidden", the input value does not appear, but when I submit the form I receive an error that a value should be present.
I have tried with option ":readonly => true", but drop down menu still appears. It appears grayed out but can still be selected.
Thank you,
Silviu
Use input hidden with same variable and value for f.assocation disabled, when submit form, the needing value will be sended at once time.
Sample code illustrate the case, note line f.association :company get the value company_id but not send, so the f.input :company_id will replace, and works!
Welcome to project railstrace !
<p>Simple Form content: </p>
<%= simple_form_for #user, url: save_path, :method => :post do |f| %>
<%= f.input :email %>
<%= f.association :company, disabled: true %>
<%= f.input :company_id, :as => :hidden, :input_html => {:value => #user.company_id} %>
<%= f.association :roles %>
<%= f.button :submit %>
<% end %>
I have a rails app with a model, products that has a nested model, productdocuments. I'm using Carrierwave to upload PDFs, Word Docs, etc .. as the documents.
In my edit.html.erb I have my form field rendering in a partial;
<%= f.file_field :resource, name: "product[productdocuments_attributes][][resource]", multiple: true, :placeholder => "Resource", id: "product_productdocuments_attributes" %>
And an array of the uploaded docs in a partial:
<%= render #product.productdocuments %>
The issue I'm seeing is that when I upload a file, rails is triggering a render of update.js.erb on the Product NOT the create.js.erb from productdocument This makes it harder to append the partial with the new productdocument.
Any idea how to to trigger create.js.erb from Productdocuments?
I guess in the edit.html.erb there's something like:
<%= form_for #product, remote: true do |f| %>
<%= f.file_field :resource, name: "product[productdocuments_attributes][][resource]", multiple: true, :placeholder => "Resource", id: "product_productdocuments_attributes" %>
<%= f.submit %>
<% end %>
which on submit sends request to the ProductsController, not to ProductDocumentsController as you expect. Since #product is a persisted record, the request goes to update method.
In this case, an additional form for product documents is going solve the problem:
<%= form_for #product.productdocuments.build, remote: true do |f| %>
<%= f.file_field :resource, , multiple: true, :placeholder => "Resource" %>
<%= f.submit %>
<% end %>
#product.productdocuments.build builds a new object, therefore request is going to create method.
Hope it helps.
I have been trying to create a form on a page, with an instance variable #next, but it doesn't seem to work. When I remove the part which includes the form, the page opens up, clearly depicting that the paths have been correctly mentioned in routes.rb. To be clear,i haev added the following method in the controller file
def new
#next=Self.new
end
The part where form is created.
<%= form_for(#next) do |a| %>
<%= a.text_field_tag :name %>
<%= a.text_field_tag :prof %>
<%= a.text_field_tag :pic, :placeholder => 'Enter address' %>
i m using Rails 4.0.2
routes.rb
get '/worms' => 'worms#index'
get '/worms/new' => 'worms#new'
post '/worms' => 'worms#create'
get '/worms/:id' => 'worms#show'
Please help.
Your form should be look like this as below...
Self means what ?, You should write the name of your model here.
And If you need full CRUD operation on the same then write the below line for the routes...
resources :your-model-name-in-plural
<%= form_for #next do |a| %>
<%= a.text_field :name %>
<%= a.text_field :prof %>
<%= a.text_field :pic, :placeholder => 'Enter address' %>
I am working with a rails form which takes users text input and sends it to the controller.
It sends two infos, the text and the language of the text (I18n.locale variable). My form looks something like that:
<%= form_for(:text, :url => {:action => 'create'} ) do |f| %>
<%= f.label :content, "#{t :"Write whatever you want"}" %><br />
<%= f.text_area :content, :cols => 80, :rows => 3 %> <br />
<%= f.hidden_field :locale, :value => I18n.locale %>
<%= f.submit "#{t :Post}"%>
<% end %>
I am sending that locale value using a hidden field. But I think this is a bad practice. User can easily modify this form. So is there any way to send that locale value among other form data automatically without any visible/hidden field?
If you want to avoid modifing data by user maybe you can use session like locale_session = "en", and use that session when you are dealing with data in controller
There are two ways of adding variable with rails form
<%= f.hidden_field :locale, :value => I18n.locale %>
AND
<%= form_for(:text, :url => {:action => 'create', :locale => I18n.locale} ) do |f| %>
But both of the above can easily modified by the user.
To avoid this either you have to use sessions or ssl page
Temporary solved this adding another line into controller. Not sure it it safe or not
def create
#text = Microblog.new(params[:text])
#text.update_attributes(params[#text.locale = "#{I18n.locale}" ])
If I have a form taking a collection of names, a date, and another boolean via virtual attributes how can I submit them to a new page and action. That has nothing to do with CRUD. It will simply do some processing then spit out the values on a new page.
personsSelection.erb
<%= render :partial => 'myForm' %>
_myForm.html
<%= simple_form_for(#person) do |f| %>
<%= f.input :names, :collection => People.all, as => :check_boxes %>
<%= f.input :DateTime %>
<%= f.check_box :paid %>
<%= f.submit %>
person_controller.erb
def personReport
#Some Random Processing
end
personReport.html.erb
#Display the personReport processing data
Again, I'm trying to submit the form to process via personReport action then display to a new page called personReport.html.erb
In your routes
match "/person/personreport" => "person#personReport", :as => personreport
In your form
<%= simple_form_for(#person, :url => personreport_path) do |f| %>
This will send the data to your personReport action where you can process the data then by default that action will render personReport.html.erb