HAML Forms being weird - ruby-on-rails

-form_tag(search_path, :method => :get)
= label_tag("Place")
= text_field_tag("place", nil, :id => "place")
= text_field_tag("near", nil, :id => "near")
= submit_tag("Search", :id => "search", :name => "search", :class => "form_submit", :disabled => false, :disable_with => "Please wait...")
I have the code above and it returns the following error:
/app/app/views/venues/main.haml:7: syntax error, unexpected kENSURE, expecting $end
My application template is as follows:
%html
%head
%title
WhosHere
= stylesheet_link_tag :all
= javascript_include_tag 'http://code.jquery.com/jquery-1.5.2.min.js'
= csrf_meta_tag
%body
= yield

maybe you should have a "do" at the end of your form_tag, so that the system knows you want to pass a block to the form_tag?
- form_tag(search_path, :method => :get) do
...

Related

HAML Form For Rails

I'm currently trying to convert an ERB layout to HAML.
This is the error I keep receiving:
index.html.haml:18: syntax error, unexpected ')'
));}\n #{_hamlout.format_...
Here is the HAML page:
.row-fluid
.span6
%h2 Todo List
.span6
%h2{:style => "text-align:right;"} <script>document.write(today)</script>
%hr.divider
.row-fluid
.span6
%h2.small_head New Task
= render :partial => 'layouts/form_errors', :locals => {:object => #list}
.form
= form_for :list, :url => {:controller => 'lists', :action => 'create'} do |f|
= label_tag :list_name, "Title", :class => 'header_label'
I have also tried this as a variation:
= form_for(:list, :url => {:controller => 'lists', :action => 'create'}) do |f|
= label_tag(:list_name, "Title", :class => 'header_label')
Neither work and both generate the same error message, and help greatly appreciated.
You need to indent the code in the do block. This should work:
= form_for :list, :url => {:controller => 'lists', :action => 'create'} do |f|
= label_tag :list_name, "Title", :class => 'header_label'

Rails 3: Text_field_tag default value if params[:something].blank?

How would one do something like:
= f.input_field :age,
:collection => 18..60,
:id => 'age',
:selected => params[:age].blank? or "20"
Above doesen't work. I want to be able to set a default value if there is no param available for that attribute.
Any smart way to do this thx!
EDIT 1:
Full form:
= simple_form_for :people, :url => request.fullpath, :method => :get, :html => { :class => 'form-search' } do |f|
#container_search_small.form-search
= f.input_field :age,
:collection => 18..60,
:id => 'age',
:selected => params[:people][:age_from] || "20"
= f.submit "Go »"
You're using helpers that are taking their values from the object you're building the form on.
So in your controller, you should preset the values on the object.
def some_action
#people = People.new
#people.age = params[:age] || 20
end
Then in the form, remove the :selected option and it should be fine. Make sure you build the form for #people :
= simple_form_for #people, :url => request.fullpath, :method => :get, :html => { :class => 'form-search' } do |f|
#container_search_small.form-search
= f.input_field :age,
:collection => 18..60,
:id => 'age'
= f.submit "Go »"

HAML - not sure why a compile error happens

I have this HAML
-content_for :primary_content do
#available-disclosures-container
.message-container.information-container
%h3=(t "headings.influencers.disclosures.new")
%p=raw((t "influencers.instructions.disclosures.new"))
=render "sidebar/disclosures/list", :disclosures => Disclosure.basics, :show_link => true, :selectable => true, :usable => false
#disclosure-form-container{:style => "display:none;"}
= form_for([:sidebar,#disclosure], :html => {:id => "disclosure-form", :remote => true}) do |f|
I am not sure why, but this last line:
= form_for([:sidebar,#disclosure], :html => {:id => "disclosure-form", :remote => true}) do |f|
is causing this error:
undefined method `model_name' for NilClass:Class
and
6: =render "sidebar/disclosures/list", :disclosures => Disclosure.basics, :show_link => true, :selectable => true, :usable => false
7:
8: #disclosure-form-container{:style => "display:none;"}
9: = form_for([:sidebar,#disclosure], :html => {:id => "disclosure-form", :remote => true}) do |f|
10: %p
11: =f.label :display_as, (t :select_disclosure_type)
12: =f.select :display_as, options_from_collection_for_select(Disclosure.basics, :display_as, :name, f.object.display_as)
Any idea what element is causing this problem?
Thanks!
This looks wrong:
= form_for([:sidebar, #disclosure]) do |f|
It looks like you're trying to generate the route by using the sidebar symbol which would need to be an instance of a model which disclosure belongs to in order to work.
Hard to know for sure but maybe this is what you're looking for:
= form_for(#disclosure), :url => [:sidebar, #disclosure] do |f|

passing ruby variable in observe_field method

I would like to know how can I pass a ruby variable inside an observe_field method.
I have the following:
<% action_parameter = params[:action] %>
<%= observe_field :car_type,
:url => { :controller => 'cars',
:action => :display_subtypes },
:with => "'id=' + value + '&somevalue=' + action_parameter"
%>
'action_parameter' is a variable and I would like to pass its value in the observe_field method but the code above does not seem to work.
Any suggestion?
try this
<% action_parameter = params[:action] %>
<%= observe_field :car_type,
:url => { :controller => 'cars',
:action => :display_subtypes },
:with => "'id=' + value + '&somevalue=#{action_parameter}'"
%>
Ruby variable will work in <% .... %>
you can use interpolation , Try this :
<%= observe_field :car_type,
:url => { :controller => 'cars',
:action => :display_subtypes },
:with => "id=#{value}&somevalue=#{action_parameter}"
%>

Haml Radio Button Pre-select Syntax

We use Ruby (1.9.2) Rails (2.3).
I'm trying to set pre-selection for radio buttons...
- form_for #user, :url => plan_user_url, :html => { :method => 'put', :class => 'form' } do |f|
- #plans.each do |p|
%span
%p= p[:blurb]
%p= p[:price]
- p[:features].each do |f|
%p= f
= f.radio_button {:id => p[:id], :checked => #user[:plan_id]==p[:id] || nil}
= f.label :plan_name, p[:name]
%p
%br
.spacer
.field.first
= f.submit 'Update', :class => 'button ok'
.field
= link_to 'Cancel', redirect_back_url || root_url, :class => 'button cancel'
HAML doesn't like this line:
= f.radio_button {:id => p[:id], :checked => #user[:plan_id]==p[:id] || nil}
Any help is appreciated.
This is invalid Ruby code:
= f.radio_button {:id => p[:id], :checked => #user[:plan_id]==p[:id] || nil}
You're attempting to call the radio_button method and Ruby thinks you're passing it a block, but really you're passing it a Hash. This is better:
= f.radio_button :id => p[:id], :checked => #user[:plan_id]==p[:id] || nil
That removes the ambiguity between Proc and Hash, but it's still weird. Why do you want the || nil? I think it's unnecessary:
= f.radio_button :id => p[:id], :checked => #user[:plan_id] == p[:id]
Thanks for the hints from Brian. It turns out
f.radio_button :plan_id, p[:id]
works for pre-select.

Resources