Rails 3.0 member routing problem - ruby-on-rails

I have a resource defined in my routes file as follows:
resources :accounts, :only => [:show, :new, :edit, :create, :update], :member => {
:profile_avatar => :get
}
In turn, in my accounts#show view I have the following code:
<%= image_tag(profile_avatar_account_path(#account, :jpg), :alt => "#{#account.username}", :title => "#{#account.username}") %>
When pulling up the page I get the following error in my production log:
ActionView::Template::Error (undefined method `profile_avatar_account_path' for #<#<Class:0x7f3fdb166260>:0x7f3fdb7bc4e8>):
Does rails 3.0 not support member anymore or is there a different way of doing this?
Thank you,
Brian

It should be
resources :accounts, :only => [:show, :new, :edit, :create, :update] do
member do
get 'profile_avatar'
end
# or
get 'profile_avatar', :on => :member
end

Try:
resources :accounts, :only => [:show, :new, :edit, :create, :update] do
get => 'profile_avatar', :on => :member
end

resources :accounts, :only => [:show, :new, :edit, :create, :update] do
member do
get :profile_avatar
end
end

Related

Ruby on Rails (Redmine) add route

Please help me add the custom route in format:
/projects/any-project-name/estimated_time/report
controller: estimatedtime
method: report
routes.rb
Rails.application.routes.draw do
root :to => 'welcome#index', :as => 'home'
.....
get '/projects/:project_id/issues/gantt', :to => 'gantts#show', :as => 'project_gantt'
get '/issues/gantt', :to => 'gantts#show'
.....
resources :time_entries, :controller => 'timelog', :except => [:show, :edit, :update, :destroy] do
get 'report', :on => :collection
end
.....
resources :time_entries, :controller => 'timelog', :only => [:new, :create]
.....
end
Try this out: get 'projects/:project_name_attribute/estimated_time/report' => 'estimatedtimes#report'

Routing in Rails fails when changing application to Multilingual Application

I am fairly new to Rails and have been unable to figure out what I am doing wrong.
Wondering if someone can help?
I have a Rails application that is working without problems, that is before I started modifications to make it multilingual.
To make it multilingual I have taken the following steps:
I added this to the routing file:
scope "(:locale)", :locale => /en|is/ do
Routing file:
PropertyEvaluator::Application.routes.draw do
scope "(:locale)", :locale => /en|is/ do
root :to => "pages#home"
get 'pages/about'
get 'pages/home'
get "log_out" => "sessions#destroy", :as => "log_out"
get "log_in" => "sessions#new", :as => "log_in"
get "sign_up" => "users#new", :as => "sign_up"
resources :searches, :only => [:index, :new, :create]
resources :users, :only => [:index, :new, :create, :edit, :update, :destroy]
resources :sessions, :only => [:new, :create, :destroy]
resources :password_resets, :only => [:new, :create, :edit, :update]
resources :email_activations, :only => [:edit]
resources :roles, :only => [:edit, :update]
end
end
Routing file before modificaions:
PropertyEvaluator::Application.routes.draw do
root :to => "pages#home"
get 'pages/about'
get 'pages/home'
get "log_out" => "sessions#destroy", :as => "log_out"
get "log_in" => "sessions#new", :as => "log_in"
get "sign_up" => "users#new", :as => "sign_up"
resources :searches, :only => [:index, :new, :create]
resources :users, :only => [:index, :new, :create, :edit, :update, :destroy]
resources :sessions, :only => [:new, :create, :destroy]
resources :password_resets, :only => [:new, :create, :edit, :update]
resources :email_activations, :only => [:edit]
resources :roles, :only => [:edit, :update]
end
To application_controller.rb I added
before_filter :set_locale
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
Rails.application.routes.default_url_options[:locale]= I18n.locale
To the view I added:
<%= link_to_unless I18n.locale == :is, "IS", locale: :is %>
|
<%= link_to_unless I18n.locale == :en, "EN", locale: :en %>
Things are working as expected with the original routing file, but when I add the scope to the routing file the problem starts.
I get the following error:
No route matches {:action=>"edit", :controller=>"users", :format=>nil, :id=>nil, :locale=>#<User id: 2, email: "bjarni.sigurdsson#bodeind.is", password_hash: "$2a$10$WzcoB5pES3TXYbWpe7xtB.yKFnqi.dhQgMXOp8/nyKi...", password_salt: "$2a$10$WzcoB5pES3TXYbWpe7xtB.", created_at: "2014-09-30 15:20:38", updated_at: "2014-10-01 12:41:57", name: "Bjarni SigurĂ°sson", auth_token: "wY2cfwk-1R7fSjCvqQPaWQ", password_reset_token: nil, password_reset_sent_at: nil, admin: true, email_confirmed: true, email_activation_token: nil, email_confirmed_at: "2014-09-30 15:20:38", role: 4>} missing required keys: [:id]
Pointing to the following code in the view:
<%= link_to(t('views.shared.navbar.edit_user_profile'), edit_user_path(current_user), class: 'btn btn-primary') %>
If I remove the scope from the routing file things work but the selected locale is not displayed in the url.
Can anyone help?
Everything is fine except passing a :locale param while path generation. Look at the exception, there's: :locale=>#<User.... Since params[:locale] is nil, it takes user as first arg. Extend your app controller with:
def default_url_options(options={})
{ locale: I18n.locale }
end

Getting ActionController::RoutingError uninitialized constant while trying to use namespace in Rails routes

I have two controllers dashboard and posts. Im trying to put the post's :new, :create, :destroy, :edit, :update actions within dashboard url like this dashboard/posts/new. But the im talking to the new action in the posts controller. Not in the dash controller. Here's my routes.rb file :
resources :posts, :except => [:new, :create, :destroy, :edit, :update]
get 'dashboard', to: 'dash#show'
namespace :dashboard do
resources :posts, :only => [:new, :create, :destroy, :edit, :update]
end
get 'dashboard/posts', to: 'dash#posts'
The two controllers in question are : dash and posts
Now when I try to visit http://localhost:3000/dashboard/posts/new it says :
ActionController::RoutingError at /dashboard/posts/new
uninitialized constant Dashboard
How to fix this?
Assuming that you have two controllers DashController and PostsController.
And you want to access few of the posts routes within the scope of namespace then you can define the routes as below:
resources :posts, :only => [:index, :show]
get 'dashboard', to: 'dash#show'
scope :dashboard do
resources :posts, controller: :posts ,:only => [:new, :create, :destroy, :edit, :update]
end
get 'dashboard/posts', to: 'dash#posts'
The constant "Dashboard" can't be found. I think the problem is that you sometimes use dash and sometimes use dashboard.

Implementing REST-api with different defaults format

I have a little REST-controller named password_resets, it has only create, show and update methods.
In routes.rb:
resources :password_resets, :only => [:create, :show, :update]
and I want one of the actions can manipulate with json by default, but others not. For all actions I can do:
scope :defaults => {format: 'json'} do
resources :password_resets, :only => [:create, :show, :update]
end
but how to do the same only for one action?
you can use multiple lines to setup differents formats:
resources :password_resets, :only => [:create, :update]
resources :password_resets, :only => [:show], :defaults => { :format => 'json' }

declarative_authorization and namespaces

Do you know - can declarative_authorization control access to namespace'd resources or not? I've tried something like
has_permission_on [:admin, :users], :to => [:index, :show, :new, :create, :edit, :update, :destroy, :search]
but it's not working :( any ideas on this?
This will work:
has_permission_on :admin_users, :to => [:index, :show, :new, :create, :edit, :update, :destroy, :search]
declarative_authorization prefixes the resource name with the namespace as [:admin, :users] could also mean that the user has permission on the admin_controller and the users_controller.

Resources