I'm using simple_form combined with twitter boostrap for my rails application. And I've one problem that stops me.
<%= f.input :title %>
<%= f.input :url %>
<%= f.input :tag_list, :label => 'Tags' %>
<%= f.input :type_id, :collection => #types, :label_method => :type_name, :value_method => :id, :include_blank => false %>
<%= f.input :description %>
That creates me nice form. But when the validation fails every input-field shows their error.
But not the select field, it only changes from a select-field into a normal input-field filled with the id and I don't know why.
Any ideas?
For some reasons, it seems Rails + Bootstrap + Simple_form keep you select field after validation when the collection is placed into the field itself instead of your controller's action. This should work :
<%= f.input :title %>
<%= f.input :url %>
<%= f.input :tag_list, :label => 'Tags' %>
<%= f.input :type_id, :collection => Type.all.order('type_name'), :label_method => :type_name, :value_method => :id, :include_blank => false %>
<%= f.input :description %>
Hope this helps.
Well I would need more information to help you out.
Could you post your models? or explain what #types is and what it is for.
This is a basic simple for association:
models/type.rb
belongs_to :post
models/post.rb
has_many :types
views/post/_form.html.haml
= f.association :types
Make sure you have a column named "title" or "name" for types.
You need to set #types in methods create or update before render action new or edit respectively
For example
def new
#your_object = YourObject.new
#types = Type.all
....
end
def create
#your_object = YourObject.new(your_object_params)
respond_to do |format|
if #your_object.save
format.html { redirect_to ... }
format.json { render action: 'show' ... }
else
### ! init data for the form ! ###
#types = Type.all
format.html { render action: 'new' }
format.json { render json: ... }
end
end
end
Related
I'm adding the gem 'nested_form' to a Rails3 app.
It works fine if you want to add the associated record and you enter data.
But, I want the associated record to be optional = no data typed in.
When I save the form, I get errors saying the associated record is missing required fields.
Client Model:
accepts_nested_attributes_for :locations
Client Form:
<%= simple_nested_form_for #client, :html => {:class => 'form-horizontal'} do |f| %>
...
<h4>Primary Location (optional) ===========</h4>
<%= f.fields_for :locations do |l| %>
<%= l.input :name, :label => 'Name' %>
<%= l.input :address1 %>
<%= l.input :address2 %>
<%= l.input :city %>
<%= l.input :state %>
<%= l.input :zipcode %>
<% end %>
...
Client Controller:
def new
#client = Client.new
#client.locations.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: #client }
end
end
You can use :reject_if parameter to make sure the record with blank required fields gets rejected. For example:
accepts_nested_attributes_for :locations, reject_if: proc { |l| l['name'].blank? && l['address1'].blank? }
Check out the documentation for more details: http://api.rubyonrails.org/v3.2.19/classes/ActiveRecord/NestedAttributes/ClassMethods.html#method-i-accepts_nested_attributes_for
I have my workers view based on users table, and I need separate item in my ActiveAdmin, so I changed default create/update logic to sth like this:
def update
if params[:worker][:password].blank?
params[:worker].delete("password")
end
#user = User.find(params[:id])
if #user.update_attributes(permitted_params[:worker])
redirect_to "/admin/workers/#{#user.id}"
else
render 'new'
end
end
But when I get the errors on my validation, I can't see any errors on page, just rendering 'new'. I have tried to something like this
resource = #user
But it didn't help.
UPDATE:
My form looks like this:
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs "Details" do
f.input :email, as: :email
f.input :first_name
f.input :last_name
f.input :street_address
f.input :apt
f.input :zip_id, :input_html => {:class => 'zip_selector'}
f.input :phone_number
f.input :is_active
f.input :description
f.input :password, :value => ''
end
f.actions
end
Any ideas ?
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 want to make chain selects part inside my Formtastic form. But is it possible to set custom ids for multiple selects for future AJAX replacement?
This doesn't work:
<%= semantic_form_for [:admin, #production_year] do |f| %>
<%= f.inputs do %>
<%= f.input :car_model, :label => "CarModel", :as => :select, :collection => Brand.find(:all, :order => "name ASC"), :id => "brand_id" %>
<% end %>
<% end %>
Options should be passed to the input_html hash, like so:
<%= f.input :car_model, :input_html => { :id => "brand_id" } %>
I have two models with a belongs_to/has_many relationship. Posts have many comments, comments belong to posts.
I need to pass the post_id through comments_controller.rb#new.
def new
#post = Post.find(params[:post_id])
#comment = Comment.new(:parent_id => params[:parent_id], :post_id => params[:post_id])
end
comment form:
<%= simple_form_for([#post, #post.comments.new]) do |f| %>
<%= f.input :post_id, :required => false, :as => :hidden %>
<%= f.input :parent_id, :required => false, :as => :hidden %>
<%= f.input :name, :label => false, :placeholder => "Name (optional)", :required => false %>
<%= f.input :content, :label => false, :placeholder => "Reply", :as => :text %>
<%= f.button :submit, "Reply" %>
<% end %>
You can pass a params from view to a controller method but I don't think you can pass a params to a controller then to a view.
In your new action, you may have to declare a #variable and define it to be your params where you can use it in your view.
But, you have already have #post.id, why can't you just use that?