serve static views from subdirs using a single controller in rails 4 - ruby-on-rails-4

If have some static pages in a tree structure. The ones at the first level are served using static_pages_controller.rb and in my routes.rb I have :
get '/static_pages/news' , :to => 'static_pages#news' , :as => 'news'
get '/static_pages/index' , :to => 'static_pages#index' , :as => 'index'
....
The above exist in
app\views\static_pages\news.html.erb
app\views\static_pages\index.html.erb
....
Now, I pasted some other static pages underneath the static_pages root:
app\views\static_pages\ermis\news.html.erb
app\views\static_pages\ermis\index.html.erb
....
I added in routes.rb this:
get '/static_pages/ermis/news' , :to => 'static_pages#news' , :as => 'news'
get '/static_pages/ermis/index' , :to => 'static_pages#index' , :as => 'index'
The above doesnt work because the actions already exist (parent folders). So I went the painful step of renaming the files to (there must be a better way though?!?)
app\views\static_pages\ermis\ermisnews.html.erb
app\views\static_pages\ermis\ermisindex.html.erb
....
and my routes.rb became
get '/static_pages/ermis/news' , :to => 'static_pages#ermisnews' , :as => 'ermisnews'
get '/static_pages/ermis/index', :to => 'static_pages#ermisindex', :as => 'ermisindex'
....
the controller is empty
class StaticPagesController < ApplicationController
end
Why cant the pages be served ? what am I missing?
When I click on
<%= link_to("Ermis", ermisnews_path, class: 'pictureTitle') %>
I get
"The action 'ermisnews' could not be found for StaticPagesController"
Here my routes.rb
Prefix Verb URI Pattern Controller#Action
root GET / static_pages#index
ermisindex GET /static_pages/ermis/index(.:format) static_pages#ermisindex
ermisnews GET /static_pages/ermis/news(.:format) static_pages#ermisnews
news GET /static_pages/news(.:format) static_pages#news
index GET /static_pages/index(.:format) static_pages#index
NOTE: I do not get an error when using link directly pointing to .erb files on static_pages
<%= link_to("News" , news_path , class: 'pictureTitle')
Question:
1) How can I use the same controller to also serve static pages underneath /static_pages eg. /static_pages/ermis
2) Am I obliged to actually rename the files to have them represent unique actions?

In route.rb, change your routes as:
resources :static_pages do
resources :ermis do
get 'ermisnews' , :on => :collection
end
end
match '/ermisnews' => 'static_pages#ermisnews', :as => :news
And then run rake routes.

Eventually I found a solution to my issue:
created the following namespace:
namespace :sp do
resources: ixanos
resources: ermis
end
created the following controllers
class Sp::IxanosController < ApplicationController
end
class Sp::ErmisController < ApplicationController
end
Placed the controllers under app/controllers/sp/
Created the directories app/views/sp/ixanos and app/views/sp/ermis and copied my files in them.
(*) This way I can have as many static pages as I want underneath the given roots (ermis and ixanos). I haven't tested the situation whereby I will have sub-directories like sp/ermis/dir1/dir2 ...

Related

Getting uninitialized constant Twilio::REST::RequestError

I keep getting the same error since I upgraded to:
gem 'twilio-ruby', '~> 5.0.0.rc4'
The call was successful set to Twilio, but the getting some error.
app/controllers/home_controller.rb:59:in `rescue in call'
require "rubygems"
require "twilio-ruby"
def call
#twilio = Twilio::REST::Client.new account_sid, auth_token
begin
#call = #twilio.account.calls.create({
:to => ,
:from => twilio_number,
:url => url,
:method => "GET",
:if_machine => "Hangup",
:timeout => "20"
})
# Getting current call status (seems like error here...!)
get_status(#call.sid)
rescue Twilio::REST::RequestError => error
#err_msg = error.message
puts #err_msg
#returned error is like below:
#NameError (uninitialized constant Twilio::REST::RequestError)
end
end
Code for getting current call status:
def get_status(sid)
#twilio = Twilio::REST::Client.new account_sid, auth_token
#call = #twilio.account.calls.get(sid)
puts "Process Status : " + #call.status
return #call.status
end
Please help to figure it out.
Thank you!
For version 5, Try Twilio::REST::RestError.
This is documented here:
There are new classes to rescue errors from. The new library now uses the Twilio::REST::RestError class.

Loading Rubywork Facets unto a Rails Application

I want to use a particular rubyworks facets unto a helper:
require 'core/facets/string/snakecase'
module GenericTableHelper
def generic_table_theadlink(head_title, order_parameter = head_title.snake_case )
render(:partial => 'common_partials/generic_table/theadlink', :locals => {:head_title => head_title,
:order_parameter => order_parameter})
end
end
I get the error:
No such file to load -- core/facets/string/snakecase
Checked gemfile:
gem 'facets'
How do I load Rubyworks Facets unto the Helper? and any other model/view/controller
I think the core is leading you astray.
require 'facets/string/snakecase`

Rails 4.2.5 can't hide routes after adding new collection route

I have a model with only two actions: list the index (watch all changes since last check) and delete all the items in the table.
As such I define a new action for the delete (in config/routes.rb):
resources :changes do
collection { delete 'delete_all' }
end
This works fine (after creating the delete_all action in the controller).
Next step is to restrict the routes that are exposed, because I only need index and the new one:
resources :changes, :only => [ :index, :delete_all ]
Unfortunately, even with this second line, the resources aren't constrained at all.
If I don't define the new action delete_all, it does work as expected.
resources :changes, :only => [ :index, :delete_all ], work with only curd action like [new,index,edit,show,destroy,create,update].
So rails resources: not allow to add custom action in ":only =>[]",and you will use with the collection.
OK I found it. Actually Rails makes a difference between the "resourceful" and "non-resourceful" routes. The resource full ones are the 7 default ones and a lot of methods only work on those.
Finally I defined the routes in as following and this worked fine:
get 'changes/' => 'changes#index', as: 'changes'
delete 'changes/' => 'changes#delete_all', as: 'delete_all'

rails4 Devise omniauth How to modify dynamically one of the strategy options?

I am using Devise+Omniauth , and I defined my own doorkeeper strategy to add a language option
In config/initializers/devise.rb , I set up :
require 'omniauth/strategies/doorkeeper'
config.omniauth :doorkeeper, Rails.application.secrets.doorkeeper_app_id, Rails.application.secrets.doorkeeper_app_secret,
:client_options => {
:site => Rails.application.secrets.doorkeeper_url
},
:authorize_params =>{:lang => I18n.locale}
which initially set lang to :en ( default locale )
this works fine and send the lang options to the remote server for Doorkeeperprocessing
now, how can I change this parameter in my client calling controller ?
I tried to use :
def index
I18n.locale = :fr
Rails.application.config.middleware.use OmniAuth::Builder do
provider :doorkeeper, :setup => lambda{|env| env['omniauth.strategy'].options[:authorize_params][:lang] = env['rack.session'][I18n.locale] }
end
but I got an error :
RuntimeError (can't modify frozen Array):
app/controllers/home_controller.rb:7:in `index'
Is there any better way to do it ? thanks for help
I modified the config/initializers/devise.rb, adding :setup => true
require 'omniauth/strategies/doorkeeper'
config.omniauth :doorkeeper, Rails.application.secrets.doorkeeper_app_id, Rails.application.secrets.doorkeeper_app_secret,
:client_options => {
:site => Rails.application.secrets.doorkeeper_url
},
:authorize_params =>{:lang => I18n.locale},
:setup => true
and I modified my doorkeeper strategy, to include the setup_phase, in which I set the lang option to the current locale.
def setup_phase
request.env['omniauth.strategy'].options[:authorize_params][:lang] = request.params["locale"]
end

Cannot use hook to remove products from homepage

I'm trying to use deface to remove the product list on the home page of a new Spree theme.
I have the following override (overrides/remove_products.rb):
Deface::Override.new(:virtual_path => "spree/layouts/spree_application",
:remove => "[data-hook='homepage_products']",
:name => "remove_products")
The override doesn't appear to run. I'm assuming that the virtual path may be incorrect? I have other overrides working (to remove the sidebar on the homepage for example).
I believe the file you are looking for is core/app/views/spree/home/index.html.erb, so I believe your virtual path should be:
spree/home/index
Example: Remove left Nav bar from Spree index page.
Step 1: create a file with name in app/overides/remove_left_nav_bar.rb
Step 2: paste following code in it.
Deface::Override.new(:virtual_path => 'spree/home/index',
:name => 'remove_left_nav_bar',
:remove => "[data-hook='homepage_sidebar_navigation']"
)
step 3: Restart your server.