I'm having a problem with associations not being available in my views.
My models are:
:user has_many :subscriptions
:subscription belongs_to :user
I'm using Devise to manage authentication etc. for Users
What I'd like to do: when creating a new user in the registration process, I also want to also create a subscription for that user.
Since Devise::RegistrationsController#new by default does not initialize an associated subscription, I've created my own RegistrationsController:
class RegistrationsController < Devise::RegistrationsController
def new
super
resource.subscriptions.build
logger.debug resource.subscriptions.inspect
end
end
The debug statement there confirms that a Subscription object is successfully created:
[#<Subscription id: nil, user_id: nil, chargify_subscription_id: nil, chargify_product_handle: nil, created_at: nil, updated_at: nil>]
The problem: in the view, resource.subscriptions does not exist.
If I inspect resource in the view, I get a User object that includes all of its own attributes but no associations (it should have an associated subscriptions)
debug(resource) gives the following:
--- !ruby/object:User
attributes:
name:
encrypted_password: ""
created_at:
updated_at:
last_sign_in_ip:
last_sign_in_at:
sign_in_count: 0 last_name:
current_sign_in_ip:
reset_password_token:
current_sign_in_at:
remember_created_at:
reset_password_sent_at:
chargify_customer_reference:
first_name:
email: ""
attributes_cache: {}
changed_attributes: {}
destroyed: false
marked_for_destruction: false
new_record: true
previously_changed: {}
readonly: false
Is there something I'm missing, or perhaps is there something strange about the resource mechanism used by Devise that prevents associations from being available in the view?
Thanks!
Edit:
If I just add resource.subscriptions.build in my view before rending the form, that works fine. But I think that kind of logic belongs in the controller and not the view, and I'd like to know what's keeping me from being able to put it there.
This answer is really late, but I found that if I override the entire controller action "new" (instead of delegating some of it to the parent with "super"), then I can build the resource properly. The reason is because "super" renders the view before handing control back to your custom controller method. Long story short...
class RegistrationsController < Devise::RegistrationsController
def new
resource = build_resource({}) # as found in Devise::RegistrationsController
resource.subscriptions.build
respond_with_navigational(resource){ render_with_scope :new } # also from Devise
end
end
Should work nicely...at least it did for me. Your code got me started on the right track anyway.
Related
Hi i am currently working on my Ruby on Rails project and i wanted to customize the devise gem but my problem is that i cant seem to find some tutorials on how to customize devise more specifically customizing the login, parameters.
I already customized the login and the view of the device , but my problem is it doesn't seem to create the username , firstname , lastnamge , age , etc.
and it only gets the email and password etc. the basic stuff
i know the documentation is great and all specially because it is complete but my problem is that i find it a bit hard to follow specially when i cant see the video or how can i customize what i can put inside in the devise if anybody can help me with this , or any information on some tutorials with actually doing what i specified above that would be great and will really be appreciated , and please i am a bit new on RoR so i find it a bit difficult in just reading the text so i need some vdeo and thanks!
simplybel#simplybel:~/projects/gamification$ rails c
Loading development environment (Rails 4.1.6)
2.1.2 :001 > User.all
User Load (0.8ms) SELECT "users".* FROM "users"
=> #<ActiveRecord::Relation [#<User id: 7, first_name: nil, last_name: nil, profile_name: nil, email: "pat#yahoo.com", encrypted_password: "$2a$10$xg.HMU2JljnVLls3IX7Go.IfJVLGYSSRePjtDaYS4nE...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2014-12-01 23:50:17", last_sign_in_at: "2014-12-01 23:50:17", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at: "2014-12-01 23:50:17", updated_at: "2014-12-01 23:50:17">]>
2.1.2 :002 >
this is the what the rails console returned after taking your advice, here is the code that you can clone
https://github.com/sanchez900/gamification.git
that is the code before I did the changes like the
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) << :username
end
end
i also did this
rails generate devise:views
also did the controllers
rails generate devise:controllers users
did the routes
devise_for :users, controllers: { sessions: "users/sessions" }
some other info might help also the project clone / link is before i did the modifications
If you had setup your view correctly to submit all the addition parameters, then you need to do as instructed in the docs https://github.com/plataformatec/devise#strong-parameters.
You need to update your permitted parameters on your sign up form to accept the new parameters you need. To do this, you can set a before filter to configure the permitted params for your devise controllers. Try something like this in your application controller.
before_action :configure_permitted_parameters, if: :devise_controller?
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:first_name, :last_name, :profile_name) }
end
you need to explicitly write it as above (or use .push if you prefer)
we're trying to get our site to have less scrapeable AND more readable urls
so e.g.
www.loomio.org/discussions/3122
becomes
www.loomio.org/d/3saA4ds9/lets-go-to-the-moon
we only really want the human-readable slug on show-links, so www.loomio.org/d/3saA4ds9/edit should be the url for editing that discussion
the solution so far follows the top answer here:
Ruby on Rails: How to override the 'show' route of a resource?
modify routes.rb:
get '/d/:id/:slug', to: 'discussions#show', as: :discussion
resources :discussions, path: 'd', except: [:edit, :show] do
get :activity_counts, on: :collection
member do
post :update_description
post :add_comment
post :show_description_history
get :new_proposal
post :edit_title
put :move
end
end
install gem FriendlyID; make and populated a :key column on Discussion table; add the following to discussion.rb (model):
KEY_LENGTH = 10
extend FriendlyId
friendly_id :key
write a custom path helper for group_path. in groups_helper.rb:
def group_url(group, options={})
url_for(options.merge(:controller => 'groups', :action => 'show',
:id => group.key, :slug => group.full_name.parameterize))
end
def group_path(group, options={})
group_url(group, options.merge(:only_path => true))
end
rake routes produces:
group GET /g/:id/:slug(.:format) groups#show
and while calling group_path(group) seems to work in some cases, I'm also seeing strange unrelated urls get generated, like :
http://loomio.org/group_requests/TegFOIx4DB/start_new_group?action=show&controller=groups%2Fgroups&slug=19-tory
in console, I'm also getting errors such as:
[5] pry(main)> g = Group.last
[6] pry(main)> app.group_path(g)
ActionController::RoutingError: No route matches {:controller=>"groups", :action=>"show", :id=>#<Group id: 2811, name: "Sylvester Buckridge", created_at: "2013-12-10 06:25:42", updated_at: "2013-12-10 06:25:42", privacy: "public", members_invitable_by: "members", parent_id: nil, email_new_motion: true, hide_members: false, beta_features: false, description: "A description for this group", memberships_count: 1, archived_at: nil, max_size: 300, cannot_contribute: false, distribution_metric: nil, sectors: nil, other_sector: nil, discussions_count: 0, motions_count: 0, country_name: nil, setup_completed_at: "2013-12-10 05:25:01", next_steps_completed: false, full_name: "Sylvester Buckridge", payment_plan: "undetermined", viewable_by_parent_members: false, key: "rkdlTytOin">}
from /home/s01us/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-3.2.16/lib/action_dispatch/routing/route_set.rb:540:in `raise_routing_error'
I've tried putting the group_path and grop_url methods in ApplicationController and ApplicationHelper to no avail.
calling
group_path( group.key, group.fullname.parameterize )
works, but would ideally like to be able to only have to call e.g.
group_path(#group)
as far as i understand the problem, you could use the good old hack with defining the to_param method on your model
class Group < ActiveRecord::Base
def to_param
"#{id}-#{slug}"
end
end
The beauty of this solution is that you won't need to do anything else. Rails will automatically use the to_param method as your record ID when it generates an URL from a record. You can do anything
redirect_to group_path(#group)
redirect_to #grup
# etc
and your Group.find should eat it as it is 123-smth-smth, usually it is smart enough to extract the integer part of the id
I am trying to insert some default users in my project using seed.rb file. I have executed the following line in the console:
rake db:seed
and no errors were thrown, but the records were not created either. When I paste the code in the rails console, again no errors are shown. I am guessing that I am doing something wrong in the seed.rb file.
This is how my models are related:
security_user.rb
has_one :security_users_detail, dependent: :destroy
has_many :security_users_manage_securities
has_many :security_users_roles, through: :security_users_manage_securities
security_users_detail.rb
belongs_to :security_user
security_users_role.rb
has_many :security_users_manage_securities
has_many :security_users, through: :security_users_manage_securities
And this is the code that I have in my seed.rb file:
users = {
Admin: {
Information: {
email: 'test#gmail.com',
password: 'test',
password_confirmation: 'test'
},
Details: {
address: 'Not defined.',
city: 'Not defined.',
country: 'Not defined.',
egn: '0000000000',
faculty_number: '',
first_name: 'Admin',
gender: 'male',
gsm: '0000000000',
last_name: 'Not defined.',
skype: 'Not defined.'
},
Roles: %w(Administrator)
}
}
users.each do |user, data|
security_user = SecurityUser.new(data[:Information])
data[:Roles].each { |role|
security_user.security_users_manage_securities.build(security_users_role: SecurityUsersRole.find_by_role(role))
}
SecurityUser.where(email: security_user.email).first_or_create!(security_user.attributes)
security_users_detail = SecurityUsersDetail.new(data[:Details])
security_users_detail.security_user_id = security_user.id
SecurityUsersDetail.where(security_user_id: security_users_detail.security_user_id).first_or_create
end
seed.rb is a rake task, so you can use puts to output messages to the console. For instance,
puts "User name: #{user.name}"
The issue was caused by the following line:
security_user = SecurityUser.new(data[:Information])
because even thought the hash passed in the new method holds only email and password fields, the result object was created with the rest of the model attributes but set to nil. For, example, I have id=>nil.
Then in I was doing the following
SecurityUser.where(email: security_user.email).first_or_create!(security_user.attributes)
in order to created the user only if exists. Anyway, since the id parameter is not in the attr_accessible clause in the model, I was not able to make mass assign.
To delete the nil values of the hash I've done the following:
SecurityUser.where(email: security_user.email)
.first_or_create!(security_user.attributes
.delete_if { |key, value| value.nil? })
Well this is my first time doing this. In routes.rb I have the following:
namespace :admin do
resources :news
end
And I have this after rake routes:
admin_news_index GET /admin/news(.:format) admin/news#index
POST /admin/news(.:format) admin/news#create
new_admin_news GET /admin/news/new(.:format) admin/news#new
edit_admin_news GET /admin/news/:id/edit(.:format) admin/news#edit
admin_news GET /admin/news/:id(.:format) admin/news#show
PUT /admin/news/:id(.:format) admin/news#update
DELETE /admin/news/:id(.:format) admin/news#destroy
As you can see the path to the action "new" is new_admin_news_path, unfortunately, when I visited that path, something pop up as follow:
No route matches {:action=>"show", :controller=>"admin/news", :id=>#<News id: nil, news_type_id: nil, title: nil, content: nil, views: nil, status: nil, start_date: nil, end_date: nil, created_at: nil, updated_at: nil, news_key: nil>}
I was thinking that the "news" is an uncountable noun and this might be the problem. So I change the config/initializers/inflections.rb to:
ActiveSupport::Inflector.inflections do |inflect|
inflect.uncountable %w( fish sheep news )
end
Obviously, it is not working...
What should I do? Any suggestions?
I have a Client and ProposalRequest model that look like this:
class Client < ActiveRecord::Base
has_many :proposal_requests
accepts_nested_attributes_for :proposal_requests, :allow_destroy => true
end
class ProposalRequest < ActiveRecord::Base
belongs_to :client
end
In my my routes file, I included the nested routes, as usual.
resources :clients do
resources :proposal_requests
end
And this is my form so far:
-semantic_form_for [Client.new, ProposalRequest.new] do |f|
=f.inputs
=f.buttons
But after this, I'm stuck because of this error.
No route matches {:controller=>"proposal_requests", :client_id=>#<Client id: nil, name: nil, title: nil, organization: nil, street_address: nil, city: nil, state: nil, zip: nil, phone: nil, email: nil, status: "interested", how_you_heard: nil, created_at: nil, updated_at: nil>}
Can anyone help me puzzle out this error?
The problem is that your nested route is meant to add a new ProposalRequest to an existing Client. If you want to create a Client and a ProposalRequest at the same time, you need to just use new_client_path and semantic_form_for #client do |f|.
I would recommend you do the following in your clients_controller:
def new
#client = Client.find(params[:id])
#client.proposal_requests.build
end
And in your view:
semantic_form_for #client do |f|
= f.inputs # fields for client
= f.inputs :name => 'Proposal Request', :for => :proposal_requests do |pf|
= pf.input :some_proposal_request_attribute
= f.buttons
Hope this helps. Make sure to look at all the examples at https://github.com/justinfrench/formtastic and do some trial and error to get your form how you want it.