I know how to create a link with a data-* attribute:
%a{ :href => "#", :data => { :name ="John", :age => 24 } } Hi John
generates:
Hi John
But how about a 2 deep data name, like data-user-name and data-user-age, a way to group data attributes. As you may guess, I tried:
:data => { :user => { :name => "John", :age => 24 } }
But it doesn't work, giving me strange HTML output:
Hi John
Any idea how to do that? Thanks in advance.
You'll have to use
:data => {'user-name' => 'John', 'user-age' => 24}
The data attribute is special-cased by HAML and it only accounts for shallow values.
Yes, you can! From the simple...
%a(data-user-name="John", data-user-last-name="Arbuckle")
To the complex
%a(data-user-name="#{User.first.name}", data-stack-overflow="all of these will be custom attributes in your link"){href: "garfield.com"}
Related
Here is my code:
base_release_id column in release db is integer type
form.html.haml
= form_for #release do |f|
= f.label :name
%br
= f.text_field :name
....
= f.label :base_release_id
%br
= f.select :base_release_id, options_from_collection_for_select(conditionsPlusBlankOrderBy(Release),"id","name",#release.base_release_id)
= f.submit
releases_controller.rb
def create
ap params
#release = Release.new(params[:release])
ap #release
...
end
I was going to create a new release including its name , base release id etc.
I was using 2 "ap" to trace the release object. strange thing happen. See my log below:
{
"utf8" => "â",
"authenticity_token" => "8HdDlC3jJxYvq+8tUh/cut5ibHxjIF6L2CzAFORlNBg=",
"release" => {
"name" => "e",
"code" => "e",
"base_release_id" => "2"
},
"commit" => "Create Release",
"action" => "create",
"controller" => "releases"
}
#<Release:0x000000190f06b8> {
:id => nil,
:name => "e",
:code => "e",
:base_release_id => nil,
}
Processing by ReleasesController#create as HTML
Parameters: {"utf8"=>"â", "authenticity_token"=>"8HdDlC3jJxYvq+8tUh/cut5ibHxjIF6L2CzAFORlNBg=", "release"=>{"name"=>"e", "code"=>"e", "base_release_id"=>"2"}, "commit"=>"Create Release"}
Not sure why base_release_id lost, name and code is working.
I have the similar code works in other page, in which case parame string value don't parsed to model integer?
it works if i added the datatype convertion as below:
def create
ap params
#release = Release.new(params[:release])
#release.base_release_id = params[:release][:base_release_id].to_i if params[:release][:base_release_id]
ap #release
...
end
Please help and thank you in advance.
found why.
in my model release.rb, i'v defined "attr_accessible :id, :name, :code, :as => :tmp_use". Then it accept id, name and code only. After i added :base_release_id, it works.
not sure how :as => :tmp_use work. I suppose default all variable can be accessed. In some places, i was using
Release.new("id"=>row[:id],"name"=>row[:name],"code"=>row[:code], :as => :tmp_use)
Some places i don't want to do it i want to all columns be accessable.
No sure how.
Thank you Ivan anyway.
I'm testing a Sinatra application, which is using DataMapper, with RSpec.
The following code:
it "should update the item's title" do
lambda do
post "/hello/edit", :params => {
:title => 'goodbye',
:body => 'goodbye world'
}
end.should change(Snippet, :title).from('hello').to('goodbye')
end
Results in this error:
title should have initially been "hello", but was #<DataMapper::Property::String #model=Snippet #name=:title>
I can of course hack this by removing the lambda and only checking if:
Snippet.first.title.should == 'goodbye'
But that can't be a long term solution since the .first Snippet may not be the same in the future.
Can someone show me the right syntax?
Thanks.
Your spec as written implies that the lambda should actually change the value of the class attribute Snippet.title; I think what you want is something like this:
it "should update the item's title" do
snippet = Snippet.first(:title => "hello")
lambda do
post "/#{snippet.title}/edit", :params => {
:title => 'goodbye',
:body => 'goodbye world'
}
end.should change(snippet, :title).from('hello').to('goodbye')
end
Right?
I finally fixed it using:
it "should update the item's title" do
snippet = Snippet.first(:title => "hello")
post "/hello/edit", :params => {
:title => 'goodbye',
:body => 'goodbye world'
}
snippet.reload.title.should == 'goodbye'
end
Thanks to #Dan Tao whose answer helped me.
How create a link_to_remote in a jQuery script where url need a parameter that is a javascript variable.
I need create a link_to_remote in pure jQuery.
Thanks in advance
You'll want to use the :with parameter with link_to_remote:
link_to_remote( args[:title],
:update => args[:update],
:url => { :action => args[:action],
:id => id,
:starting => args[:starting]
},
:with => "'filter[viewer_id]=' + $('filter_viewer_id').value",
:loading => "Element.hide('#{args[:update]}');Element.show('#{args[:loading]}')",
:complete => "Element.show('#{args[:update]}');Element.hide('#{args[:loading]}')" )
Notice how I am sending the filter_viewer_id by getting it's value from a form field with jQuery. If you don't need that level of detail, just pass the name of your javascript variable.
Like this:
<%= link_to_remote('Hello', :url => "/test?id='+ id +'&test=true") %>
This will result in:
# => Hello
Very little info on your post. Are you using Rails 3? If so, did you check the jquery-rails gem out?
Best regards,
-- J. Fernandes
When you have a one-to-many association in Rails 3 and accept nested attributes with delete, is it possible to know by looking at the objects (the associated object) whether it's going to be deleted or not?
For example:
group.attributes = {:member_attributes => {"0" => {:id => 1, :name => "John"},
"1" => {:id => 2, :name => "Dave"},
"2" => {:id => 3, :name => "Gus", "_destroy" => true}}}
Is it possible by looking at group.members to know that the one with id 3 is going to be deleted on save?
There's a method for finding that out, called marked_for_destruction?
group.members.each do |member|
puts "#{member.name} => #{member.marked_for_destruction?}"
end
would generate
John false
Dave false
Gus true
I have the following line in a view:
<%= f.select(:province_id, options_from_collection_for_select(Province.find(:all, :conditions => { :country_id => #property.country_id }, :order => "provinces.name ASC"), :id, :name) %>
In the province model I have the following:
def name
I18n.t(super)
end
Problem is that the :name field is translated (through the province model) and that the ordering is done by activerecord on the english name. The non-english result set can be wrongly sorted this way. We have a province in Belgium called 'Oost-Vlaanderen'. In english that is 'East-Flanders". Not good for sorting:)
I need something like this, but it does not work:
<%= f.select(:province_id, options_from_collection_for_select(Province.find(:all, :conditions => { :country_id => #property.country_id }, :order => "provinces.I18n.t(name) ASC"), :id, :name) %>
What would be the best approach to solve this?
As you may have noticed, my coding knowledge is very limited, sorry for that.
You need to sort the entries after they have been loaded from the database. This should probably do the trick:
Provinces.find(:all, :conditions => {:country_id => #property.country_id}).sort_by{|p| p.name}