How do I make a simple form association have pre data when editing?
My form:
<h1>Editing kategori</h1>
<%= simple_form_for(#konkurrancer, :url => {:action => 'update', :id => #konkurrancer.id }) do |f| %>
<%= f.association :kategoris %>
<%= f.button :submit, :value => 'Edit konkurrence' %>
<% end %>
You almost did it:
<%= f.button :submit, :value => 'Edit konkurrence', :data_pre => "whatever here" %>
Your best bet is to do something like this in your controller:
def edit
#konkurrancer = Konkurrancer.new(:title => "hello wurld", :description => "blah blah blah", :nuclear_level => 10)
end
Then your form should automatically populate these values as "pre data".
Related
When I submit a remote form in rails, the form POST duplicate data, even though if I manually serialize the form using jQuery shows that there are no duplicates.
What could be causing this?
*Normally this wouldn't be a problem, but one of the form data is an array, as a result there are duplicate value in the submitted array.
Result from chrome's network inspector
Result from jQuery
UPDATE
Here is the gist of my form (removing the html markups):
<% #order.available_payment_methods.each do |method| %>
<%= radio_button_tag "order[payments_attributes][][payment_method_id]", method.id, method == #order.available_payment_methods.first %>
<%= Spree.t(method.name, :scope => :payment_methods, :default => method.name) %>
<%= render :partial => "spree/checkout/payment/#{method.method_type}", :locals => { :payment_method => method } %>
<% end %>
# partial
<% param_prefix = "payment_source[#{payment_method.id}]" %>
<%= label_tag "name_on_card_#{payment_method.id}", Spree.t(:name_on_card) %><span class="required">
<%= text_field_tag "#{param_prefix}[name]", "#{#order.billing_firstname} #{#order.billing_lastname}", { id: "name_on_card_#{payment_method.id}", class:'form-control'} %>
<% options_hash = Rails.env.production? ? {:autocomplete => 'off'} : {} %>
<%= text_field_tag "#{param_prefix}[number]", '', options_hash.merge(:id => 'card_number', :class => 'required cardNumber form-control', :size => 19, :maxlength => 19, :autocomplete => "off") %>
<%= text_field_tag "#{param_prefix}[expiry]", '', :id => 'card_expiry', :class => "required cardExpiry form-control", :placeholder => "MM / YY" %>
<%= text_field_tag "#{param_prefix}[verification_value]", '', options_hash.merge(:id => 'card_code', :class => 'required cardCode form-control', :size => 5) %>
<% if #order.bill_address %>
<%= fields_for "#{param_prefix}[address_attributes]", #order.bill_address do |f| %>
<%= render :partial => 'spree/address/form_hidden', :locals => { :form => f } %>
<% end %>
<% end %>
<%= hidden_field_tag "#{param_prefix}[cc_type]", '', :id => "cc_type", :class => 'ccType' %>
UPDATE Submitting the form without remote: true fixes the problem (BUT I NEED IT!)
Add onsubmit="return false();" on your form it will make sure that form is not submited through default form submission. It will be submitted through ajax as you are adding remote: true
I have been trying to pass what round the users intends to go to by which button they click through a hidden field that passes in the round number.
<%= form_for #round, :url => { :action => 'pick_page'} do |f| %>
<%= f.hidden_field :round, :value => '1', :class =>'round1' %>
<%= f.submit 'Picks', :class => 'round1' %>
<%= f.hidden_field :round, :value => '2', :class =>'round2' %>
<%= f.submit 'Picks', :class => 'round2' %>
<% end %>
With this code I constantly get 2 passed through as the round in my pick_page. It is obviously skipping over the first hidden field. How can I get it so 'f.submit' submits the round number that is associated with its class.
We just had the same problem! We fixed it creating 2 diferent forms right next to each other.
<%= form_for #round, :url => { :action => 'pick_page'} do |f| %>
<%= f.hidden_field :round, :value => '1', :class =>'round1' %>
<%= f.submit 'Picks', :class => 'round1' %>
<%= f.hidden_field :round, :value => '2', :class =>'round2' %>
<%= f.submit 'Picks', :class => 'round2' %>
<% end %>
Change it to
<%= form_for #round, :url => { :action => 'pick_page'} do |f| %>
<%= f.hidden_field :round, :value => '1', :class =>'round1' %>
<%= f.submit 'Picks', :class => 'round1' %>
<% end %>
<%= form_for #round, :url => { :action => 'pick_page'} do |f| %>
<%= f.hidden_field :round, :value => '2', :class =>'round2' %>
<%= f.submit 'Picks', :class => 'round2' %>
<% end %>
You can just remove the hidden fields and add a name and value attributes on your submit button.
I have a form wizard setup with 4 steps that all work on the same model.
When I fill out the first one and click submit (and am redirected to step 2) the form is already filled out and a duplicate, identical form added below. This happens for each step.
How can i render a completely new form after i click submit on step 1 and am taken to 2 (or 3/4)?
step 1
<%= simple_form_for #user, url: wizard_path do |f| %>
<h2 class="signup">Step 3: Friends Birthday</h2>
<%= f.simple_fields_for :events do |event_f| %>
<%= event_f.input :name, :placeholder => 'Enter Persons Name' %>
<%= event_f.input :date, :as => :date_picker, :input_html => { :class => 'special' } %>
<%= event_f.input :reccuring, :label => false, :inline_label => 'Remind me of this event every year?' %>
<h3>Choose up to 3 interests for this person</h3>
<%= event_f.association :interests, :label => false, :as => :check_boxes %>
<%= event_f.input :kind, :as => :hidden, :input_html => { :value => "Birthday" } %>
<%end%>
<%= link_to "skip this step", next_wizard_path %>
<%= f.button :submit, 'Submit' %>
<%end%>
step 2
<%= simple_form_for #user, url: wizard_path do |f| %>
<h2 class="signup"> Married? Add your anniverarsy</h2>
<%= f.simple_fields_for :events do |anniversary_f| %>
<%= anniversary_f.input :name %>
<%= anniversary_f.input :date, :as => :date_picker, :input_html => { :class => 'special' } %>
<h3>Choose up to 3 interests for this person</h3>
<%= anniversary_f.association :interests, :as => :check_boxes, :label => false %></li>
<%= anniversary_f.input :kind, :as => :hidden, :input_html => { :value => "Anniversary" } %>
<%end%>
<%= link_to "skip this step", next_wizard_path %>
<%= f.button :submit, 'Submit' %>
<%end%>
Controllers
events_controller.rb
def new
#user = current_user
#event = Event.new
end
def create
#user = current_user
#event = Event.new(params[:event])
#event.user = User.find(current_user.id)
end
users_controller
class UsersController < ApplicationController
before_filter :authenticate_user!
def create
#user = User.new(params[:user])
if #user.save
redirect_to user_steps_path
else
render :new
end
end
When you complete one step then intilize new object and bind this second form to new object.Repeat this step for 3rd and 4th form.
Mean every time initilize
#user = User.new
I would like to create an edit page for the below form. The problem is that when the user browses to the edit page the brand_name and name are pre-filled, but the image upload field shows 'no file chosen' even when an avatar exists for the 'style'. Please let me know if there is some way to remedy this. Thanks!
My Edit Form:
<%= simple_form_for #style, :html => { :class => 'form-horizontal' }, :remote => true do |m| %>
<%= m.input :brand_name, :label => 'Brand', :placeholder => 'Brand' %>
<%= m.input :name, :label => 'Style', :placeholder => 'Style' %>
<%= m.input :avatar, :label => "Image" %>
<div class="form-actions" style = "background:none">
<%= m.submit nil, :class => 'btn btn-primary' %>
<%= link_to 'Cancel', styles_path, :class => 'btn' %>
</div>
<% end %>
Just implemented this yesterday. Make a custom input in /app/inputs
class AvatarInput < SimpleForm::Inputs::FileInput
def input
out = '' # the output string we're going to build
# check if there's an uploaded file (eg: edit mode or form not saved)
if object.send("#{attribute_name}?")
# append preview image to output
# <%= image_tag #user.avatar.url(:thumb), :class => 'thumbnail', id: 'avatar' %>
out << template.image_tag(object.send(attribute_name).url(:thumb), :class => 'thumbnail', id: 'avatar')
end
# append file input. it will work accordingly with your simple_form wrappers
(out << #builder.file_field(attribute_name, input_html_options)).html_safe
end
end
Then you can do
<%= f.input :avatar, :as => :avatar %>
This is all I needed for this to work (in haml):
=simple_form_for #user, :html => {:multipart => true } do |f|
=f.file_field :image
The code for new/edit views from paperclip's github page looks like this:
<%= form_for :user, #user, :url => user_path, :html => { :multipart => true } do |form| %>
<%= form.file_field :avatar %>
<% end %>
So maybe you should try m.file_field and include :html => { :multipart => true } as well? Though I personally prefer Attachment-Fu.
'shared/subscription' %>
To call this partial view:
<% form_for(:subscription, :url => city_subscriptions_path(#city)) do |form| %>
<%= form.hidden_field :city_id, :value => #city.id %>
<%= form.text_field :email, :size => 30 %>
<%= form.submit "Email Me" %>
<% end %>
Since I am using this partial view on different places, how do I alter the caller so it will pass a hash for the form_for helper? So it would be like this when the helper is called:
<% form_for(:subscription, :url => city_subscriptions_path(#city), :html => {:id => 'main_input' }) do |form| %>
<%= form.hidden_field :city_id, :value => #city.id %>
<%= form.text_field :email, :size => 30 %>
<%= form.submit "Email Me" %>
<% end %>
<%= render :partial => "shared/subscription", :locals => {:foo => "bar", :foofoo => ["bar", "bar"]}
In your partial view, use them:
<%= foo #this outputs "bar" %>
<%= foofoo.to_s %>