RoR set default value for text field while using observe - ruby-on-rails

I am trying to set a default value for a text field used for a search, which uses the observe method. Here's what I got which works.
<% form_tag('javascript:void(0)') do %>
<%= text_field_tag 'phrase', nil, {:onfocus => "$('results').show();", :onblur => "$('results').hide();"} %>
<%= observe_field :phrase,
:frequency => 0.5,
:update => 'results',
:url => {:controller => :profiles, :action => 'search', :only_path => false },
:with => "'phrase=' + encodeURIComponent(value)" %>
<% end %>
This works fine, but obviously the value is nil.
Now if I add in the value like so:
<% form_tag('javascript:void(0)') do %>
<%= text_field_tag 'phrase', :value => 'test', {:onfocus => "$('results').show();", :onblur => "$('results').hide();"} %>
<%= observe_field :phrase,
:frequency => 0.5,
:update => 'results',
:url => {:controller => :profiles, :action => 'search', :only_path => false },
:with => "'phrase=' + encodeURIComponent(value)" %>
<% end %>
An exception is thrown.
Any idea on how I can get a default value for this text field?
Thank you.

Do not use :value => 'test', simply use 'test':
<%= text_field_tag 'phrase', 'test' %>
The format you tried to use is for text_field helper that is usually coming from a model, text_field_tag does not typically come from a model.

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? %>

Updating paperclip avatar in multipart simple_form

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.

RnR using params[] getting hash when I want value

I have a web form to which I want to take a parameter from the URL and add it to the web form as a hidden field on the form.
<%= form_tag(:action => 'create') do |f| %>
<%= text_field_tag :email,"Your email address...", :class => "text", :id => "email", :name => 'email',
:onFocus => "change(this,'#222222'); this.value=''; this.onfocus=null;", :size => "26" %>
<%= hidden_field_tag :ref_code, :id => 'ref_code', :name => 'ref_code', :value => params[:ref_code] %>
<%= submit_tag "Enter To Win", :class => "button-positive submit" %>
<% end %>
If I just do a:
<%= params[:ref_code] %>
I get the value I want which is a five character alphanumeric, however when I use it in the form, I get the full hash description:
{:id=>"ref_code", :name=>"ref_code", :value=>["k53e5", "home", "index2"]}
Why? I tried .values, .to_s, and other ways of getting by key and I always get the full hash instead of just the value. What am I doing wrong? Thanks.
http://apidock.com/rails/ActionView/Helpers/FormTagHelper/hidden_field_tag
hidden_field_tag(name, value = nil, options = {})
<%= hidden_field_tag :ref_code, params[:ref_code], { :id => 'ref_code', :name => 'ref_code' } %>

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 .

tinymce in ajax call - how to get contents

this is what I have in my view:
<% form_remote_tag :url => { :controller => 'comments', :action => "create", :post_id => "#{#post.id}"}, :html => {:id => 'comment_form' },
:before => "tinyMCE.triggerSave(true,true);" do %>
<%= label_tag 'Comment' %><br/>
<%= text_area_tag :comment_body, nil,:rows => 10, :cols => 100 %><br/>
<p style="margin-top: 10px;">
<%= submit_tag 'Add',:id => 'btnCommentSave' %>
</p>
<% end %>
Tinymce editor is displayed properly.
In my controller:
how to get the contents of the text area?
I am expecting the contents in params[:comment_body] and I am not seeing it?
I tried doing this also,
$('#btnCommentSave').click( function(){
tinyMCE.triggerSave(true,true);
$('#comment_form').submit();
});
What am I missing?
Thanks
try this and see if it makes a difference
tinyMCE.get(id_of_the_text_area).save()

Resources