Rails 3.1 Formtastic input custom id or class - ruby-on-rails

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" } %>

Related

Rails form used for new and edit - set todays date vs existing

I have a Rails app that uses the same events/_form.erb for new and editing an event.
If it's a new event, I want to pre-select today's date. If it's being edited, I want to have it show the already entered date.
I thought I could do this with a flash setting.
This is the events/new.html.erb:
<% flash[:newevent] = "true" %>
<%= render :partial => 'form' %>
This is the code in events/_form.html.erb
<% if flash[:newevent] = "true" %>
<%= f.input :starts_at, :as => :string, :label => 'Date', :input_html => {:class => 'datepicker', :value => Date.today.to_s} %>
<% else %>
<%= f.input :starts_at, :as => :string, :label => 'Date', :input_html => {:class => 'datepicker'} %>
<% end %>
Thanks for the help!
Is this you're looking for ?
... :value => f.object.starts_as || Date.today.to_s
controller:
#new_event = true # or false in edit
view:
<% if #new_event %>
or just this in in view:
<% if f.object.new_record? %>

how to get an image to show in a formtastic :radio, :collection?

I can't get an image to show in a Formtastic form with radio buttons. Here's my form. The :activity_id, :as => radio is the issue:
<%= semantic_form_for #event do |f| %>
<%= f.inputs do %>
<%= f.input :date, :as => :string, :input_html => { :class => 'jquery-ui-date'} %>
<%= f.input :date, :as => :hidden, :input_html => { :id => 'date-alt'} %>
<%= f.input :activity_id, :as => :radio, :collection => Activity.all %>
<% end %>
<%= f.actions do %>
<%= f.action :submit, :as => :button %>
<%= f.action :cancel, :as => :link %>
<% end %>
<% end %>
That works fine as far as showing the radio button, the activity name, and saving correctly on submit. But I need to show the image that goes with the activity because most of my users cannot read and must rely on images and TTS. I've got the images showing in my other views.
I've tried dozens of combinations of things with :hint, and :wrapper_html, and image_tag and so on. So many variations I've become scrambled in the brain-pan.
A sample attempt:
<%= f.input :activity_id, :as => :radio, :collection => Activity.all, :hint => f.template.image_tag(f.object.image_url(:thumb)) %>
This gives me an "undefined method `image_url'" error. Yet this :hint works fine elsewhere.
I'll switch back to a standard html.erb view if that helps, but my problem there was a "stringify_keys" error I was unable to solve.
There's gotta be a way. Please? Thanks much. . .

rails simple_form two models

I'm starting to use simple_form for a rails application, and while converting some of my forms, I came across one that has two models that it is working with, sort of an embedded form. Is this possible with simple_form?
<% simple_form_for :topic, :url => forum_topics_path do |t| %>
<%= t.input :name, :label => 'Topic' %></p>
<p>First Post:<br/></p>
Title: <%= text_field :post, :title %> <--- this is where i start having problems
Body: <%= text_area :post, :body %>
<%= t.submit 'Save' %>
Thanks
Use simple_fields_for :
<%= simple_form_for :topic, :url => forum_topics_path do |topic_builder| %>
<%= topic_builder.input :name, :label => 'Topic' %>
<%= topic_builder.simple_fields_for :post do |post_builder| %>
<p>First Post:</p>
<%= post_builder.input :title, :input_html => { :size => 30 } %>
<%= post_builder.input :body, :as => :text, :input_html => { :rows => 20, :cols => 50, :class => 'resizable' } %>
<% end %>
<%= topic_builder.submit 'Save' %>
<% end %>
Notes
Note the = symbol in <%= simple_form_for ... and <%= simple_fields_for (required in Rails 3.x)
Removed "Title:" and "Body:" text. Use the label generated for the inputs and style their location with CSS as needed.
Added example of using input_html
There's another approach that I'm using and it works great. Ryan Bates (RailsCasts) has created a gem to handle this.
See https://github.com/reu/simple_nested_form for the details.

how to send a parameter not associated with the model object using form_for in rails

I use form_for to save a model object site
<% form_for :site :url => {:controller => 'site', :action => 'add_site' } do |f| -%>
<%= f.text_field :protocol, :size => 127, :style => 'width:255px' , :value => "http://"%>
<%= f.text_field :site_name, :size => 127, :style => 'width:255px' , :value => "www."%>
<%= f.hidden_field :user_id, :value => #user.id %>
<%= f.hidden_field :status, :value => 'Not Verified' %>
<% end -%>
here the field protocol is not a instance of the model site. but i just want to pass to the action add_site so that i can use as params[:protocol]
what should i do to achieve this?
You can set something like this:
<%= text_field_tag :protocol %>
To the action of the controller you can refer to this as params[:protocol]
Add it to your site model as an attribute accessor:
attr_accessor :protocol
See http://railscasts.com/episodes/16-virtual-attributes .

How to pre-check checkboxes in formtastic

I have a form I'm trying to set up ...
Users can have many posts, and each post can have many people watching it.
The Watch model is set up polymorphically as 'watchable' so it can apply to different types of models. It has a user_id, watchable_id, watchable_type and timestamps as attributes/fields.
This is soley so that when people comment on a post, users watching the post can get an email about it.
What I'm trying to do is show the user a list of users that they can tag on each post, which is not problem. This is what I'm using right now
http://pastie.org/940421
<% semantic_form_for #update do |f| %>
<%= f.input :subject, :input_html => { :class => 'short' } %>
<%= f.input :site, :include_blank => false, :input_html => { :class => 'short' } %>
<label>Tag Users (they will get notified of this update)</label>
<%= f.input :user, :as => :check_boxes, :label => ' ', :wrapper_html => { :class => "radiolist clearfix" }, :for => :watch, :name => "Watch" %>
<%= f.input :note, :label => 'Update'%>
<% f.buttons do %>
<%= f.commit_button :button_html => { :class => 'submit' } %>
<% end %>
<% end %>
The problem with this, is that when you go to edit an update/post ... all the checkboxes are prechecked ... I want it to pre-check only users who are currently watching the post.
To further clarify ... this is the hacky way I'm getting it to do what I want right now
<ul class="radiolist clearfix">
<% #users.each do |user| %>
<li>
<%= check_box_tag 'user_ids[]', user.id, #watches.include?(user.id) ? true : false -%>
<%= h user.name -%>
</li>
<% end %>
</ul>
where #watches is just an array of user ids
#watches = #update.watches.map{|watcher| watcher.user_id}
For anyone else having the same issue:
<%= f.input :some_input, :as => :boolean, :input_html => { :checked => 'checked' } %>
For multiple check boxes, this way:
<%= f.input :tags, :as => :check_boxes, :collection => some_map.collect { |c| [c[:name], c[:id], {:checked=> tag_ids.include?(c[:id])}] } %>
If you need the state of the checkbox to reflect the value of :some_input
<%= form.input :some_input, :as => :boolean, :input_html => { :checked => :some_input? } %>
In your model..
def some_input?
self.some_input ? true : false
end
Set the boolean attribute's value to 'true' in the controller before your render the form. That should make Formtastic default the checkbox to 'checked'.

Resources