Syntax error in haml - ruby-on-rails-3

Im getting syntax error for the following haml
= javascript_include_tag :reports
= stylesheet_link_tag :form
= stylesheet_link_tag :tab
.tabcontainer
%ul.tabs
%li#reports
Reports
.tab
.field
= form_for #report, {:html => {class => "form"}} do |f|
.field
= f.label :Property
b
%form{ :action => "/reports/find/employees", :method => "get"}
%fieldset
%ol
%li
%label{:for => "property"} Property:
%input{:type => "text", :name => "query[1][property]" }
%li
%label{:for => "comp"} Comparator:
%input{:type => "text", :name => "query[1][comp]" }
%li
%label{:for => "value"} Value:
%input{:type => "text", :name => "query[1][value]"}
%input{:type => "submit", :value => "Go" }
The error im getting is :
index.html.haml:12: syntax error, unexpected tASSOC
haml_temp = form_for #report, {:html => {class => "form"}} do |f|
^
index.html.haml:12: syntax error, unexpected '}', expecting keyword_end
haml_temp = form_for #report, {:html => {class => "form"}} do |f|
^
index.html.haml:17: syntax error, unexpected keyword_ensure, expecting $end
Extracted source (around line #12):
9: .tab
10: .field
11:
12: = form_for #report, {:html => {class => "form"}} do |f|
13: .field
14: = f.label :Property
15: b
Whats causing this error ?
Please Help
Thank You.

You're missing a :, should be:
haml_temp = form_for #report, {:html => {:class => "form"}} do |f|

At form tag there is 'html=> {some code}, you missed : for `class

Related

How to query three different tables to find out specific information

Ok, this is a triple table query, and I am very very confused in how to go about this, let along construct it.
I'm trying to find all the tutoring session tied to a specific email address.
The tables information is listed down beneath.
User, TutorSession, & SessionUser.
TutorSession belongs to User
Users has many SessionUser
Here are the attributes of each table:
User
:id => :integer,
:email => :string,
:encrypted_password => :string,
:reset_password_token => :string,
:reset_password_sent_at => :datetime,
:remember_created_at => :datetime,
:sign_in_count => :integer,
:current_sign_in_at => :datetime,
:last_sign_in_at => :datetime,
:current_sign_in_ip => :string,
:last_sign_in_ip => :string,
:created_at => :datetime,
:updated_at => :datetime,
:first_name => :string,
:last_name => :string,
:provider => :string,
:uid => :string,
:roles_mask => :integer,
:last_activity_at => :datetime,
:time_zone => :string,
:imported => :boolean,
:authentication_token => :string,
:getting_started_dismissed => :boolean,
:schedule_valid => :datetime
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
add_index "users", ["first_name"], :name => "index_users_on_first_name"
add_index "users", ["last_name"], :name => "index_users_on_last_name"
add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
TutorSession
:id => :integer,
:session_code => :string,
:name => :string,
:moderator_pw => :string,
:attendee_pw => :string,
:session_type => :string,
:status => :string,
:started_at => :datetime,
:ended_at => :datetime,
:tutor_pickup_type => :string,
:recording_url => :string,
:created_at => :datetime,
:updated_at => :datetime,
:requested_by => :integer,
:subject_full_name => :string,
:student_dismissed => :boolean,
:school_id => :integer,
:tutoring_subject_id => :integer,
:tutor_appointment_id => :integer,
:white_board_status => :string,
:session_length => :integer
SessionUser
t.integer "tutor_session_id"
t.integer "user_id"
t.string "usertype"
t.string "url"
t.datetime "login_at"
t.string "logout_at"
t.string "flag"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "user_agent_string"
t.string "browser"
t.string "os"
end
add_index "session_users", ["tutor_session_id"], :name => "index_session_users_on_session_id"
add_index "session_users", ["user_id"], :name => "index_session_users_on_user_id"
EDIT
I made a mistaking in stating how the tables were related to each other. Sorry, my head is somewhere else.
SessionUser belongs to both user and tutor_session
TutorSession has many session_users
User has many session_users
Not sure what your model looks like, but should be as easy as:
User.find_by_email('someone#example.com').tutor_sessions
For this to work your models should look something like this:
class User < ActiveRecord::Base
has_many :sessions, :class_name => SessionUser
has_many :tutor_sessions, :through => :sessions
end
Basically, this query:
SELECT s.*
FROM users u
JOIN session_user su ON u.id = su.user_id
JOIN tutor_session s ON s.id = su.tutor_session_id
WHERE u.email = 'foo#bar'

Netzke column locking

I saw a feature in Ext JS document - http://dev.sencha.com/deploy/ext-4.0.1/examples/grid/locking-grid.html. We can lock a column by set the option "locked" according the document, but when I did this in Netzke Grid, the grid disappears. Is there any specific method to achieve this. Below the sample code is given,
def configure(c)
super
c.model = "Product"
c.title = "Product List"
c.columns = [
{
:name => :name,
:text => "Name",
:read_only => true,
:locked => true
},
{
:name => :price,
:text => "Price"
},
{
:name => :date,
:text => "Date"
}
]
end

Sign up page/form in Rails 3 not considering all fields required/mandatory

My sign up page has login. email, password, password_confirm and company fields . All of them are supposed to be mandatory but when i click on sign up it is not checking if any fields except password and password_confirm are blank!! I dont understand what is the difference as in my app/models/user.rb file i have validates presence for all fields. Please help me
Here's code(required lines)
user.rb
class User < ActiveRecord::Base
has_many :excel_files # One user may have many excel files
has_one :user_access_validity# One user may have one license period
# Virtual attribute for the unencrypted password
attr_accessor :password
attr_accessible :login
attr_accessible :email
attr_accessible :password
attr_accessible :password_confirmation
attr_accessible :company
#changes of 'validates' in accordance with rails 3:
validates :login, :presence => true,
:length => { :within => 3..40},
:uniqueness => { :case_sensitive => false },
:format => { :with => /^([a-z_0-9\.]+)$/i },
:on => :create,
:if => :is_login_entered?
validates :email, :presence => true,
:length => { :within => 7..100},
:uniqueness => { :case_sensitive => false },
:format => {:with => /^([a-z]+((\.?)|(_?))[a-z0-9]+#(mindtree.com|rvce.edu.in))$/i},
:on => :create,
:if => :is_email_entered?
validates :company, :presence => true,
:format => { :with =>/(mindtree|RVCE)/i},
:format => { :with => /^([a-z]+)$/i },
:on => :create,
:if => :is_company_entered?
validates :password, :presence => true,
:length => { :within => 4..40 },
:confirmation => true,
:format => { :with => /^([a-z0-9#!#\$]+)$/i },
:on => :create,
:if => :password_required?
validates :password_confirmation, :presence => { :if => :password_required? }
before_save :encrypt_password
#
# is_email_entered? : checks whether the email field is entered or not
# #params : none
# #return : true - if the email is entered
# false - if the email is not entered
#
def is_email_entered?
!self.email.blank?
end
#
# is_company_entered? : checks whether the company field is entered or not
# #params : none
# #return : true - if the company is entered
# false - if the company is not entered
#
def is_company_entered?
!self.company.blank?
end
#
# is_login_entered? : checks whether the login field is entered or not
# #params : none
# #return : true - if the login is entered
# false - if the login is not entered
#
def is_login_entered?
!self.login.blank?
puts "login"
end
protected
#
# password_required? : Checks whether either of the crypted_password and password field is blank
# #params : none
# #return : true - if either of the crypted_password and password field is blank
# false - if either of the crypted_password and password field is not blank
#
def password_required?
crypted_password.blank? || !password.blank?
puts "in pr?"
end
end
controller.rb
def signup
if logged_in?
flash[:notice] = "<span class='error'>You have already registered with us!</span>".html_safe
redirect_to :action => 'upload_file'
else
#user = User.new(params[:user])
return unless request.post?
#user.save!
self.current_user = #user
random_number = #user.generate_random_number
puts random_number
new_random_number = "c" + #user.id.to_s + random_number
#user.customerid = new_random_number
#user.created_at = get_current_datetime
# #user.updated_time = ''
#user.save
# save user's maximum access days
user_validity = UserAccessValidity.new
user_validity.user_id = self.current_user.id
user_validity.maximum_access_in_days = 90
user_validity.save
# redirect_back_or_default(:controller => '/account', :action => 'welcome')
redirect_to :controller => '/account', :action => 'welcome'
flash[:notice] = "<span class='success'>Thanks for registering!</span>".html_safe
end
end
signup.html.erb
<font color=red>(Fields marked * are mandatory)</font><h3>Sign me up!</h3>
<br>
<span class='error'><%= error_messages_for (#user) %></span>
<%= form_for :user do |f| -%>
<span class='error'><%= flash[:msg] %></span>
<p><label for="login"><span class='redcolor'>*</span>Login</label><br/>
<%= f.text_field :login %></p>
<p><label for="email"><span class='redcolor'>*</span>Email</label><br/>
<%= f.text_field :email %></p>
<p><label for="password"><span class='redcolor'>*</span>Password</label><br/>
<%= f.password_field :password %></p>
<p><label for="password_confirmation"><span class='redcolor'>*</span>Confirm Password</label><br/>
<%= f.password_field :password_confirmation %></p>
<p><label for="company"><span class='redcolor'>*</span>Company</label><br/>
<%= f.text_field :company %></p>
<p><%= f.submit 'Sign up', :name=> 'sign_up' %></p>
<% end -%>
The problem is that you pass an if option to your validation which also applies to the presence validation. Take for example the following
validates :login,
:presence => true,
:length => { :within => 3..40},
:uniqueness => { :case_sensitive => false },
:format => { :with => /^([a-z_0-9\.]+)$/i },
:on => :create,
:if => :is_login_entered?
is_login_entered? is called when validating the presence of login which conflicts with the validation which is why the validation is skipped. Change your validation to
validates :login, :presence => true, :on => :create
validates :login,
:length => { :within => 3..40},
:uniqueness => { :case_sensitive => false },
:format => { :with => /^([a-z_0-9\.]+)$/i },
:on => :create,
:if => :is_login_entered?
This way, the other validations will only run if a login is present.

Update action fire an insert query?

Hi i know it's silly but true i got this when i edit any entry.
I have an expense model and an expense_line_item and a paid_line_item model, it is created properly when i create a new entry but on editing previous entry it adds a new entry instead of updating, i. e. at update action it fires insert query. here is my code:
My controller:
def new
#menu = 'Expenses'
#page_name = 'Record New Expenses'
#expense = Expense.new
#expense.expense_line_items.build
#expense.paid_line_items.build
#expense.voucher_number = "EXP"+Time.now.to_i.to_s
#from_accounts = TransactionType.fetch_from_accounts(current_company.id, 'payments')
#to_accounts = TransactionType.fetch_to_accounts(current_company.id, 'payments')
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #expense }
end
end
# GET /expenses/1/edit
def edit
#menu = 'Expenses'
#page_name = 'Edit Expenses Entry'
#expense = Expense.find(params[:id])
#from_accounts = TransactionType.fetch_from_accounts(current_company.id, 'payments')
#to_accounts = TransactionType.fetch_to_accounts(current_company.id, 'payments')
end
# POST /expenses
# POST /expenses.xml
def create
#expense = Expense.new(params[:expense])
#expense.created_by = current_user.id
#expense.company_id = current_company.id
respond_to do |format|
if #expense.save
format.html { redirect_to(#expense, :notice => 'Expense was successfully created.') }
format.xml { render :xml => #expense, :status => :created, :location => #expense }
else
#menu = 'Expenses'
#page_name = 'Record New Expenses'
#from_accounts = TransactionType.fetch_from_accounts(current_company.id, 'payments')
#to_accounts = TransactionType.fetch_to_accounts(current_company.id, 'payments')
format.html { render :action => "new" }
format.xml { render :xml => #expense.errors, :status => :unprocessable_entity }
end
end
end
# PUT /expenses/1
# PUT /expenses/1.xml
def update
#expense = Expense.find(params[:id])
respond_to do |format|
if #expense.update_attributes(params[:expense])
format.html { redirect_to(#expense, :notice => 'Expense was successfully updated.') }
format.xml { head :ok }
else
#menu = 'Expenses'
#page_name = 'Edit Expenses Entry'
format.html { render :action => "edit" }
format.xml { render :xml => #expense.errors, :status => :unprocessable_entity }
end
end
end
My Model:
expense model:
class Expense < ActiveRecord::Base
has_many :expense_line_items
has_many :paid_line_items
accepts_nested_attributes_for :expense_line_items, :reject_if => lambda {|a| a[:account_id].blank? } , :allow_destroy => true
accepts_nested_attributes_for :paid_line_items, :reject_if => lambda {|a| a[:account_id].blank? }, :allow_destroy => true
#validations
validates_presence_of :expense_date, :voucher_number
validates_presence_of :expense_line_items
validates_associated :expense_line_items
validates_presence_of :paid_line_items
validates_associated :paid_line_items
end
expense_line_item:
class ExpenseLineItem < ActiveRecord::Base
belongs_to :expense
end
paid_line_item:
class PaidLineItem < ActiveRecord::Base
belongs_to :expense
end
My form :
<%= form_for(#expense) do |f| %>
<% #expense.expense_line_items.each_with_index do |expense_line_item, index| %>
<%= render "expense_line_items", :expense_line_item => expense_line_item, :index => index %>
<% end %>
<tr id="row_link" valign="top">
<td valign="top" colspan="6">
<%= link_to "Add new row",{:action => :add_row, :index => #expense.expense_line_items.size}, :remote => true %>
</td>
</tr>
<% #expense.paid_line_items.each_with_index do |paid_line_item, index| %>
<%= render "paid_line_items", :paid_line_item => paid_line_item, :index => index %>
<% end %>
<tr id="to_row_link" valign="top">
<td valign="top" colspan="6">
<%= link_to "Add new row",{:action => :add_to_row, :index => #expense.paid_line_items.size}, :remote => true %>
</td>
</tr>
<% end %>
i got frustrated, thanks in advance .
i have found a solution for this problem. When i tried to update a lineitem it take it as a new one hence i have to pass a hidden lineitem_id for update action. i have used
below code :
<%= hidden_field_tag "expense[expense_line_items_attributes][#{index}][id]",expense_line_item.id%>
and it works for me.

rails3.1 and formtastic 2.0.0.rc2 - undefined method `inputs'

I am using rails 3.1.0.rc3 with formtastic 2.0.0.rc2 and I am getting this error -
undefined method `inputs' for #<ActionView::Helpers::FormBuilder:0x000001059c2fb0>
Here is the block of code
= form_tag '#', :class => 'formtastic' do
= fields_for CustomFields::Field.new, :builder => Formtastic::Helpers::FormHelper.builder do |g|
= g.inputs :name => :attributes do
= g.input :_alias
= g.input :hint
= g.input :text_formatting, :as => 'select', :collection => options_for_text_formatting, :include_blank => false, :wrapper_html => { :style => 'display: none' }
= g.input :target, :as => 'select', :collection => options_for_association_target, :include_blank => false, :wrapper_html => { :style => 'display: none' }
Is this a bug ?
Thanks, Alex
You are trying to use a formtastic method here. When you are actually in a block for Rails's form builder.
You need semantic_form_for and formtastic in your Gemfile to use f.inputs for example..