link_to_remote equivalent in rails 4 - ruby

I am upgrading from rails2.3.5 to rails4 and i am facing difficulty to convert link_to_remote into rails 4
link_to_remote("Retire", :url => { :action => :delete, :id => user.id },
:condition => "confirm_retire(#{user.id})", :html => {:style => 'color: #CC0000;float:right;margin-right:8px;'})
I know that we need for this :remote => true with link_to like
link_to('Retire', {:action => :delete, :id => user.id},
:condition => "confirm_retire(#{user.id})", :remote => true,
:class => 'retire')
But i am facing problem with :condition => "confirm_retire(#{user.id})",
how to handle this ?, i did lot of research but not found any solution

The condition option isn't supported by link_to. You could perform that check in a js file using a data attribute on the link element to provide a value for the condition.
link_to('Retire', {:action => :delete, :id => user.id},:remote => true, :class => 'retire', :data-user-retire => "your_condition")
$('.retire').on('click', function(){
if ($(this).attr('data-user-retire') == "your condition") {
return true;
} else {
$(this).append("some message")
return false;
}
});
This is just an example, you'll need to amend it for your requirements.

Related

Can't get value from nested pair in Omniauth hash.

I'll try to keep this simple, my previous wording was maybe a bit too verbose:
Here is the example Omniauth hash: https://github.com/mkdynamic/omniauth-facebook
I can access and save some values from this but not others. The field is writable, so I know its just my syntax (beginner, sorry!)
{
:provider => 'facebook',
:uid => '1234567',
:info => {
:nickname => 'jbloggs',
:email => 'joe#bloggs.com',
:name => 'Joe Bloggs',
:first_name => 'Joe',
:last_name => 'Bloggs',
:image => 'http://graph.facebook.com/1234567/picture?type=square',
:urls => { :Facebook => 'http://www.facebook.com/jbloggs' },
:location => 'Palo Alto, California',
:verified => true
},
:credentials => {
:token => 'ABCDEF...', # OAuth 2.0 access_token, which you may wish to store
:expires_at => 1321747205, # when the access token expires (it always will)
:expires => true # this will always be true
},
:extra => {
:raw_info => {
:id => '1234567',
:name => 'Joe Bloggs',
:first_name => 'Joe',
:last_name => 'Bloggs',
:link => 'http://www.facebook.com/jbloggs',
:username => 'jbloggs',
:location => { :id => '123456789', :name => 'Palo Alto, California' },
:gender => 'male',
:email => 'joe#bloggs.com',
:timezone => -8,
:locale => 'en_US',
:verified => true,
:updated_time => '2011-11-11T06:21:03+0000'
}
}
}
I can do this to get gender and save it.
location:auth.extra.raw_info["gender"]
Obviously though I dont want to save gender to location. I want to get "Palo Alto" and save it. But this doesn't work.
location.auth.extra.raw_info["location"]["name"]
What am I doing wrong? When I try it in console, I'm able to get the value.
try this
location.auth.extra.raw_info.location.name
or this
location.auth.extra.raw_info[:location][:name]
Yeah, what you suggested was what I was trying...and it turns out we were right but FB had changed how that hash was set up so it wasn't working. Lesson learned: subscribe FB's notifications next time :)

How do I correctly call Magento SOAP API with Ruby Savon for Category Product Links?

I am trying to call the catalog_product_link.list API method using Savon. However, I keep receiving the error Error cannot find parameter.
Here is what I am using, though I have tried several variations of the call and still cannot get it to go through correctly:
client = Savon.client(wsdl: 'http://localhost/index.php/api/soap/?wsdl')
response = client.call(:login){message(username: 'user', apiKey: 'key')}
session = response.body[:login_response][:login_return]
#These all do not work
client.call(:call){message(:session => session, :method => 'catalog_product_link.list', :type => 'up_sell', :productId => '166')}
client.call(:call){message(:session => session, :method => 'catalog_product_link.list', :type => 'up_sell', :product => '166')}
client.call(:call){message(:sessionId => session, :resourcePath => 'catalog_product_link.list', :args => {:type => 'up_sell', :product => '166'})}
client.call(:call){message(:sessionId => session, :resourcePath => 'catalog_product_link.list', :args => {:type => 'up_sell', :productId => '166'})}
client.call(:call){message(:sessionId => session, :resourcePath => 'catalog_product_link.list', :arguments => {:type => 'up_sell', :product => '166'})}
Is there a different way to format to get this to work?
UPDATE: If I try removing the type parameter, it gives the error Given invalid link type, so it appears it does not like something about multiple parameters.
response = client.call(:call){message(:session => session, :method => 'catalog_product_link.list', :product => '166')}
I was able to get this to work using Builder:
class ServiceRequest
def initialize(session, type, product)
#session = session
#type = type
#product = product
end
def to_s
builder = Builder::XmlMarkup.new()
builder.instruct!(:xml, encoding: "UTF-8")
builder.tag!(
"env:Envelope",
"xmlns:env" => "http://schemas.xmlsoap.org/soap/envelope/",
"xmlns:ns1" => "urn:Magento",
"xmlns:ns2" => "http://xml.apache.org/xml-soap",
"xmlns:xsd" => "http://www.w3.org/2001/XMLSchema",
"xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance"
) do |envelope|
envelope.tag!("env:Body") do |body|
body.tag!("ns1:call") do |call|
builder.sessionId(#session, "xsi:type" => "xsd:string")
builder.resourcePath("catalog_product_link.list", "xsi:type" => "xsd:string")
builder.args("xsi:type" => "ns2:Map") do |args|
args.item do |item|
item.key("type", "xsi:type" => "xsd:string")
item.value(#type, "xsi:type" => "xsd:string")
end
args.item do |item|
item.key("product", "xsi:type" => "xsd:string")
item.value(#product, "xsi:type" => "xsd:string")
end
end
end
end
end
builder.target!
end
end
client.call(:call, xml: ServiceRequest.new(session, 'up_sell', '166').to_s)
Thanks to #rubiii for the direction.

Assign custom labels to checkboxes using Formtastic

I have a few checkboxes coming from nested model (:admin accepts_nested_attributes_for :account_setting). By default, the label for the checkbox is generated from the model's attributes. But I want to use custom labels which I store in the locales folder as key value pairs.
This is the code,
= semantic_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :validate => true, :html => { :method => :put }) do |f|
= f.input :email
= f.inputs :receive_email_digest, :as =>:check_boxes, :for => :account_setting, :label => 'My custom label'
And it does not work. I tried :input_html, :member_label.
Does formtastic suppor this? Or do we have to hack it?
inputs is a fieldset. You're telling formtastic to make a fieldset and ordered list with one list item and input, :receive_email_digest, which I presume is a boolean. The :as here is not really having any effect, and :check_boxes is for has_many associations.
You want something like:
= semantic_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :validate => true, :html => { :method => :put }) do |f|
= f.inputs do
= f.input :email
= f.inputs :for => :account_setting do
= f.input :receive_email_digest, :label => 'My custom label'
which will render two fieldsets each with a single input. Or perhaps even:
= semantic_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :validate => true, :html => { :method => :put }) do |f|
= f.inputs "Email Settings" do
= f.input :email
= f.semantic_fields_for :account_setting do |ff|
= ff.input :receive_email_digest, :label => 'My custom label'
The semantic fields for here is merely a scope and shouldn't output any markup.
I believe both of these will use en.formtastic.labels.<resource_name>.account_setting.receive_email_digest for the check box's label, where resource name is whatever your resource's underscored name is. Check out the formtastic source for how these keys are generated.

Rails, route mapping not matching the correct action

I'm trying to route the following URL:
/shop/{category_id}/{product_id}/
to a controller action called 'product'
I've got this in my routes:
match "/shop/:category/:id" => "shop#product"
and i've got link_to's as follows:
link_to_unless_current "#{t('murals')}", url_for(:controller => 'shop', :category => 'walls', :id => 'murals')
When the urls are output, they are:
http://0.0.0.0:3000/shop?category=walls&id=murals
instead of the desired:
http://0.0.0.0:3000/shop/walls/murals
if i switch the route, so it goes to:
match "/shop/:category/:id" => "shop#index"
this works, but it's not the right action.
Any help would be appreciated.
try this
<%= link_to_unless_current "#{t('murals')}", custom_path(:category => 'walls', :id => 'murals') %>
match "/shop/:category/:id" => "shop#product", :as => :custom

link_to method post is not working Rails 2.3.9

I'm trying to implement some link_to's using post method.
However, it always puts all the information as a query string in the browser's bar (it behaves as a GET).
Why?
Here is my code:
<%= link_to hotel[:name], {:controller => "gds_hotels", :action => "hotel_details",
:dest => #destination,
:ci => #check_in,
:co => #check_out,
:hotel => hotel,
:rooms => hotel[:rooms]}, :method => "POST" %>
Thank you! =)
You should use button_to
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to

Resources