View overriding using Deface - spree

I am trying to insert a text on show page of Spree's product page.
My app/overrides/show_new.html.erb as follows:
Deface::Override.new(:virtual_path => "spree/products/show",
:insert_after => "[data-hook='product_show']",
:text => "<p>This is overriding......</p>",
:name => "show_new",
:disabled => false)
But, the view is not overridden. Am I missing something?

You must restart your app for the overrides to register first.
Otherwise, this seems correct (assuming your code is in app/overrides/something.rb)
Watch out if you are not on latest version, some views HTML are broken and it breaks deface.
https://github.com/spree/spree/issues/1056 (I ran into this issue last week)

Related

Radio button not working in rails form

I want to create a form where i will be having 2 radio buttons. one for 'public' and onother for 'private'.
I have a User model in my project and i have privacy field in it which is boolean and its default value is true(that is 'public' in my case).
I have update button in my form. I want to submit the form and accordingly want to update the value in User model. and then if whenever i visit edit page the corresponding radio button should be selected.
This is my form
= form_tag "/users/abc" do
%p.heading We Respect Your Privacy
.privacy
%input#r1{:name => "privacy", :type => "radio", :value => 0}
%label.privacyLabel{:for => "r1"}
%span>
Friends Only
%p
TraveLibro enables you to save your travel memories and share them with your friends. While other TraveLibro users
can see your itineraries, only your friends on TraveLibro can view your personal pictures.
%br
%p Now you can upload all your captured memories without ever having to be concerned about your privacy.
.privacy
%input#r2{:name => "privacy", :type => "radio", :value => 1}
%label.privacyLabel{:for => "r2"}
%span>
Everyone
%p
Want to share your travel tales with the world. With an open profile TraveLibro users can view the itineraries
you have created and the pictures that complete those memories.
.privacyUpdate
%a{:href => "javascript:void(0);"}
%input.privacyUpdateBtn{:type => "submit", :value => "update"}/
and this is my controller action
def abc
current_user.update_column('privacy', params[:privacy] == 0 ? 0 : 1)
redirect_to :back
end
But its not working
Try this:
def abc
current_user.update_column('privacy', params[:privacy].to_i == 0 ? 0 : 1)
redirect_to :back
end

Globalize: How to show in the view the current locale information

I am currently having a bit of a problem with Globalize gem.
I explain the current situation:
I have a Model called Question. After creating it, without any data stored, I added the following lines to the model:
class Question < ActiveRecord::Base
translates :wording, :answer1, :answer2, :answer3, :answer4
end
Then, I created a migration to create the translations table
class CreateTranslationsTable < ActiveRecord::Migration
def up
Question.create_translation_table! :wording => :string, :answer1 => :string, :answer2 => :string, :answer3 => :string, :answer4 => :string
def end
def down
Question.drop_translation_table!
def end
My default locale is :en. After that I added some data.
If I go execute rails c and put the command Question.first.wording everything works fine.
Although when I execute in 'rails c' I18n.locale = :es and then I do Question.first.wording still displays the english text I put at the beginning.
I tried one thing which it seemed to help me is that I dropped all the translated columns (like specified in Globalize documentation after you migrated data. In my case I didn't have any data to migrate at the beginning though). After that I made a rollback (which got back the columns I deleted form the Question model), then executing Question.first.wording with I18n.locale = :es got it working. Which means that Question.first.wording returns nil.
After that, I implemented the 'Locale from Url Params' as specified in the Ruby on Rails guide
Which means the first URL param si the ':locale' param.
Now the current problem: The view still displays the information in English when it should display it in Spanish, since the URL I entered was http://localhost.com/es/questions/.
How can I make it to display in the view the Spanish information?
My mistake. I interpreted from the documentation that the the chunck of code (in application_controller.rb) that works for setting the url:
def default_url_options(options={})
{ locale: params[:locale] }
end
would actually set the 'I18n.locale' variable. What I did is the next to get around this (in application_controller.rb):
before_action :change_to_current_locale
def change_to_current_locale
I18n.locale = params[:locale]
end
That made it work.

Rails 4 and Active Admin: ActiveModel::ForbiddenAttributesError

The full error is the following:
ActiveModel::ForbiddenAttributesError in Admin::ProductsController#create
My product model only has a name and price. Why is commit a parameter? When I click the 'Create Product' button within the admin dashboard, this is the params output:
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"6/pCeklsaik4sYF5h8+WRPddkH7wJn9ZJHd6YLaaNuc=",
"product"=>{"name"=>"Black Shirt Male",
"price"=>"25"},
"commit"=>"Create Product"}
From what I've gathered reading other Stack Overflow posts, you need to use strong parameters in Rails 4 instead of attr_accessible, which was done for me when I scaffolded the product model. In my create action in the Products controller, I have:
#product = Product.new(product_params)
And product_params is defined as:
def product_params
params.require(:product).permit(:name, :price)
end
I didn't do anything fancy when I created the model, and in my Gemfile I'm using the following as the documentation suggested for Rails 4:
gem 'stripe', :git => 'https://github.com/stripe/stripe-ruby'
Why am I getting this error when I create a new product via the Active Admin dashboard? Any input on the matter is appreciated.
Alright figured it out. I'm not sure if this is the 'correct' way but the products are being created.
in app/admin/product.rb I did:
permit_params :list, :of, :attributes, :on, :model, :name, :price
where
permit_params :list, :of, :attributes, :on, :model
was initially commented out. So I just added :name and :price
Question already answered, but I'm adding this as a helpful resource to supplement this answer:
https://github.com/activeadmin/active_admin/blob/master/docs/2-resource-customization.md#setting-up-strong-parameters

Why does FB ignore the picture, name, and caption parameter when posting a link on a user's stream?

I have my app posting a link to the user's wall if they have given it permissions, but it ignores the picture, name, and caption values I pass along with it.
HTTParty.post("https://graph.facebook.com/#{#sponsorship.fbid}/links", :query => {:access_token => URI.escape(#access_token), :link => URI.escape(request.env['HTTP_HOST']), :picture => URI.escape("#{request.env['HTTP_HOST']}/images/fb_post/after_donation.jpg"), :name => URI.encode("Click here"), :caption => URI.encode("This is the caption")})
Any help is appreciated, thanks.
Think you might need to be using
https://graph.facebook.com/#{#sponsorship.fbid}/feed
instead of
https://graph.facebook.com/#{#sponsorship.fbid}/links
It's probably taking those values from the open graph meta tags on the link target rather than the ones you provide at post time. Not sure if that's supposed to be happening but you should check if your link has the correct tags.
https://developers.facebook.com/docs/opengraph/ is the docs and https://developers.facebook.com/tools/debug will show you what markup is detected on your page

Rails 3 routes with extended regex :constraints - bug?

I have the following route in my Rails 3 app.
post 'games/:id/:task/:card_id' => 'games#perform', :as => :perform
...which allows, obviously, such requests as button_to("Foo", {card_id=>2, :action=>:perform, :task=>"foo"}), mapping to the URL /games/1/foo/2.
I'd like to restrict the set of tasks the route matches. The Rails API docs indicate that "Constraints can include the ‘ignorecase’ and ‘extended syntax’ regular expression modifiers". However, the following works as expected:
post 'games/:id/:task/:card_id' => 'games#perform', :as => :perform, :constraints => {:task => /(foo|bar)/}
But the following doesn't:
post 'games/:id/:task/:card_id' => 'games#perform', :as => :perform, :constraints => {:task => /(foo|
bar)/x}
In the latter case, the button_to link above produces the URL: /games/perform?card_id=2&task=foo.
Is this a bug, or am I doing something wrong?
Yeah, there's a bug in the RegexpWithNamedGroups processing for Ruby 1.8.x because it fakes the named regexp groups syntax of 1.9 in a way that is incompatible with regexp modifiers.
The problem here is that with 1.8 Rails uses a simple index of the paren group within the regexp to map to the name. When you add a modifier to a regexp, internally this creates an additional group which throws the index off by one.
Workaround right now is to use 1.9, or no modifier.