How can i prepopulate a text area with rails - ruby-on-rails

I want to preset a textarea with a value but it isn't working.
<%= f.text_area(:self_summary, :input_html => { "date-pre" => "I will get to this later."}, :class => "textareastyle") %>

<%= f.text_area(:self_summary, :value => "I will get to this later.", :class => "textareastyle") %>

Related

Simple form format a time field to show '21:30' instead of entire UTC time string

My rails _form code:
<%= simple_form_for #admin_event, :html => { :class => 'main-form' } do |f| %>
<%= f.input :time_of_event, :format => l(:time_of_event, '%d %b. %Y'), :as => :string, :label => 'Hora', :input_html => { :class => 'thin', :placeholder => 'eg: 20:30' } %>
<% end %>
I'm getting the error message:
Cannot convert symbol to string.
How can I set up this _form to always display this field using a time converter so while the database has a full field (2000-01-01 19:30:00.000000), in the forms it would only show 19:30?
Solved this with the following: created the method "time_format" on my ApplicationHelper:
def time_format(datetime)
datetime.strftime('%H:%M') unless datetime.blank?
end
And then on the form:
<%= f.input_field :start_time, :as => :string, :value => time_format(f.object.start_time) %>
Hope this will help you.
You can add attr_accessor to your model (for example formatted_time) and use this field for get fromatted time and set it. Before save you can parse value of #formatted_time and apply it to time_of_event field.

Using form_for multi-select fields with acts_as_taggable_on

I can't figure out the right code for using a predetermined set of options for a multi-select field. I want to have a list of skills in a drop down that users can select from. Here is the code I am using, it works fine as a single select field, but not as a multi-select:
<%= form_for(#user, :html => { :class => "form-stacked" } ) do |f| %>
...
<div class="clearfix"><%= f.select :skill_list, options_for_select(["Asst", "dir", "pres"]),
{
:multiple => true,
:class => "chzn-select",
:style => "width:450px;" } %></div>
...
<% end %>
Anyone have any suggestions? Eventually, I will want to store all of the options for the multi-select form elsewhere because there will be a bunch, but this is the first challenge I can't figure out..
Thanks.
EDIT
I have also tried:
:html => { :multiple => true, :class => "chzn-select", :style => "width:450px;" } and it doesnt work either
There needs to be two pairs of brackets, one for the options, and one for html_options, like so:
<%= f.select :skills_list, options_for_select(["Asst", "dir", "pres"]), {}, {:multiple => true, :class => "chzn-select", :style => "width:450px;" } %>
See the docs for the select helper.

How can I send subject parameter to controller using remote_function?

Suppose this form:
<% form_for(#student) do |f| %>
<%= f.select(:subject_id,
options_from_collection_for_select(#subjects, :id, :name),
{:prompt => 'Select a subject' },
{:onChange => "#{remote_function(:update => :student_exam_id,
:url => {
:action => :update_exams_list,
:subject_id => 1
}
)
}" } ) %>
<%= f.select(:exam_id,
options_from_collection_for_select(#exams, :id, :title) ) %>
<% end %>
When user selects a subject, then exams selector list must be updated with exams belongs to selected subject.
How can I send subject parameter to controller? I tried to send parameter using :subject_id => 1, but it does not work.
Please suggest me some ways to make this.
If you need more info, please ask me.
Thank you very much.
Greetings.
Hey, I could answer your question with details. Instead you should only watch this screencast:
http://railscasts.com/episodes/88-dynamic-select-menus
It explanins what you need and with more detailed instructions.
Hope it helps.
I changed my select:
<%= f.select(:subject_id,
options_from_collection_for_select(#subjects, :id, :name),
{:prompt => 'Select a subject' },
{:onChange => remote_function(:update => :student_exam_id,
:url => { :action => :update_exams_list },
:with => "'subject_id=' + this.value") } ) %>
and it works. :D

How do you make each option in a drop down menu a link with the association simple_form call?

I have this form using the simple_form plugin:
<%= simple_form_for([#video, #video.comments.new], :remote => true) do |f| %>
<%= f.association :comment_title, :collection => #video.comment_titles, :label => "Comment Title:", :include_blank => false %>
<%= f.input :body, :label => false, :placeholder => "Post a comment." %>
<%= f.button :submit, :value => "Post" %>
<% end %>
and this creates a drop down list with this line:
<%= f.association :comment_title, :collection => #video.comment_titles, :label => "Comment Title:", :include_blank => false %>
My question is how do you modify this code so that each drop down item is a link to each comment_title's individual show view?
UPDATE
Here is the generated html from the code from the first answer:
<select class="select optional" id="comment_comment_title_id" name="comment[comment_title_id]">
<option value="<a href=" comment_titles="" 224"="">#<CommentTitle:0x10353b890>">#<CommentTitle:0x10353b890></option>
<option value="<a href=" comment_titles="" 225"="">#<CommentTitle:0x1035296e0>">#<CommentTitle:0x1035296e0></option>
<option value="<a href=" comment_titles="" 226"="">#<CommentTitle:0x1035295a0>">#<CommentTitle:0x1035295a0></option>
</select>
I actually figured it out. Here is the ruby code:
<%= f.association :comment_title, :collection => #video.comment_titles.map {|ct| [ct.title, comment_title_path(ct)] }, :label => "Comment Title:", :include_blank => false %>
This passes the first element in the array as the text, and the second element in the array as the value. Then I use this jQuery code:
$("select").change(function () {
var url = $("select option:selected").val();
$(location).attr("href",url);
});
Pretty simple.
try :collection => #video.comment_titles.map {|ct| [ct, (link_to ct, comment_title_path(ct))] }

POSTing via RoR getting back JSON, now what?

OK, I'm totally new to Ruby on Rails. I created a form that posts to an external widget that returns JSON. So I have this form:
<%= form_for :email, :url => 'http://XXX.XX.XXX.212/widgetapi.0.1.php', :html => {:class => "new_email"} do |f| %>
<%= f.text_field :email, :value => "Your email address...", :class => "text", :id => "email", :name => 'email',
:onFocus => "change(this,'#222222'); this.value=''; this.onfocus=null;",
:size => "26" %>
<%= f.hidden_field :apiKey, :id => "apiKey", :name => 'apiKey', :value => "ABC123" %>
<%= f.hidden_field :lrDomain, :id => "lrDomain", :name => 'lrDomain', :value => "signup.triplingo.com" %>
<%= f.hidden_field :urlPrefix, :id => "refCodeUrl", :name => 'refCodeUrl', :value => "http://signup.website.com/" %>
<%= f.hidden_field :ref_code, :id => 'ref_code', :name => 'ref_code', :value => #referralid %>
<%= submit_tag "Enter To Win", :class => "button-positive submit" %>
<% end %>
Which works. Now I get back a JSON response that is:
({"email":"testing#testing2.com","reflink":"fi1ts","newuser":true})
Ok now the result it the browser sits on the response page with the JSON.
I'm guessing I need to do something with the #response in the controller, but I'm not sure what. All I want to do is if that "newuser" is true, provide them a success page. If false, go to an error page.
Thanks.
You should change that form, so that it will send request to your controller. In that controller you should do API Call with parameters from a form(using for example Curb: https://github.com/taf2/curb or Net/http: http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/index.html). Then You can parse result in JSON and show user correct page.
So:
1) User send request to YOUR application
2) YOUR application make request to http://XXX.XX.XXX.212/widgetapi.0.1.php using data from user
3) YOUR application receives JSON, and check if newuser is true.
4) If it is true action render success page, otherwise it render error page.

Resources