I have this set up in my ActivitiesController:
class ActivitiesController < ApplicationController
def index
following_ids = current_member.following_members.map(&:id)
#activities = Activity.where("member_id in (?)", following_ids.push(current_member.id)).order("created_at desc")
end
end
This set up in my FollowsController:
def create
#member = Member.find_by_user_name(params[:member_id])
#follow_member = current_member.follow(#member)
if #follow_member
current_member.create_activity(#follow_member, 'followed')
respond_to do |format|
format.html { redirect_to #member }
format.js
end
end
end
And this set up to display the activity of following another member:
<div>
<span class="status_name">
<%= link_to activity.member.user_name, profile_path(activity.member) %>
</span>
<span>
is now following <%= link_to activity.targetable.following_member.user_name, "#" %>
</span>
<span class="meta">
<%= time_ago_in_words(activity.targetable.created_at) %>
</span>
</div>
<div class="act_content">
<%= follow_profile_link activity.targetable.following_member %>
</div>
I was able to get the following action to create a new activity item in the feed just fine but I can't figure out how to call to the member who's being followed. I get this error:
undefined method `following_member' for #<Follow:0x835b150>
app/views/activities/follow/_followed.html.erb:3:in `_app_views_activities_follow__followed_html_erb__269116874_68857716'
app/views/activities/index.html.erb:14:in `block in _app_views_activities_index_html_erb___16061925_44845644'
app/views/activities/index.html.erb:5:in `_app_views_activities_index_html_erb___16061925_44845644'
I can't figure out what I need to do or what specifically to call to make this work. Any ideas?
EDIT - Models
Activity Model
class Activity < ActiveRecord::Base
belongs_to :member
belongs_to :targetable, polymorphic: true
end
Follow Model
class Follow < ActiveRecord::Base
extend ActsAsFollower::FollowerLib
extend ActsAsFollower::FollowScopes
# NOTE: Follows belong to the "followable" interface, and also to followers
belongs_to :followable, :polymorphic => true
belongs_to :follower, :polymorphic => true
def block!
self.update_attribute(:blocked, true)
end
end
Member Model
class Member < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :email_confirmation, :password, :password_confirmation, :remember_me,
:first_name, :last_name, :user_name, :pursuits, :avatar, :bio, :city, :state, :country, :pursuit_list
has_many :medium
has_many :statuses
has_many :activities
acts_as_follower
acts_as_followable
acts_as_ordered_taggable
acts_as_ordered_taggable_on :pursuits
def create_activity(item, action)
activity = activities.new
activity.targetable = item
activity.action = action
activity.save
activity
end
Edit - Activities Migration
class CreateActivities < ActiveRecord::Migration
def change
create_table :activities do |t|
t.integer :member_id
t.string :action
t.integer :targetable_id
t.string :targetable_type
t.timestamps
end
add_index :activities, :member_id
add_index :activities, [:targetable_id, :targetable_type]
end
end
Activity Index
<% #activities.each do |activity| %>
<div class="status">
<div class="row">
<div class="span1 stream_av">
<%= avatar_profile_link activity.member %>
</div>
<div class="span7">
<%= render partial: "activities/#{activity.targetable_type.underscore}/#{activity.action}",
locals: { activity: activity } %>
</div>
</div>
</div>
<% end %>
New activity for uploading media
<div>
<span class="status_name">
<%= link_to activity.member.user_name, profile_path(activity.member) %></span> <span>uploaded new <%= link_to "Media", profile_media_path(activity.member) %></span> <span class="meta"> <%= time_ago_in_words(activity.targetable.created_at) %>
</span>
</div>
<div class="act_content">
<%= link_to image_tag(activity.targetable.asset.url(:medium), class: ''), medium_path(activity.targetable_id) %>
</div>
New activity for writing a status
<div>
<span class="status_name">
<%= link_to activity.member.user_name, profile_path(activity.member) %></span> <span>wrote a <%= link_to "Post", profile_stream_path(activity.member) %></span> <span class="meta"> <%= time_ago_in_words(activity.targetable.created_at) %>
</span>
</div>
<div class="act_content">
<%= activity.targetable.content %>
</div>
following_member returns an ActiveRecord object. So, you gotta do something like:
activity.targetable.following_member.first.user_name
instead of
activity.targetable.following_member.user_name
If you want to retrieve all the following members, you'll have to iterate through it.
<%activity.targetable.following_member.each do |member| %>
<%=member.user_name%>
<%end%>
Related
As I searched, this is a common issue, but none of the answers I found work for my case.
I have set up a User model with devise and it has two related models, it has one Contact Detail and many Addresses. The nested form works well with addresses, but my contact detail fields are not shown.
My User model is the following:
validates_presence_of :contact_detail, :addresses
# Include default devise modules. Others available are:
# :lockable, :timeoutable, :trackable and :omniauthable
devise :invitable, :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable, :confirmable
has_one :contact_detail, dependent: :destroy
has_many :addresses, dependent: :destroy
accepts_nested_attributes_for :addresses,
allow_destroy: true
accepts_nested_attributes_for :contact_detail,
allow_destroy: true
The contact details model only has belongs_to :user
I made the changes mentioned at devise gem at my application controller:
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [addresses_attributes: [:street_name, :street_number, :city, :country, :postal_code, :name],
contact_detail_attributes: [:first_name, :last_name, :description, :telephone, :mobile ]])
end
end
and my app/views/devise/registrations/new.html.erb file looks like this:
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
...
<div>
<% f.fields_for :contact_detail do |contact_detail|%>
<div class="field">
<%= contact_detail.label :first_name %>
<%= contact_detail.text_field :first_name %>
</div>
<div class="field">
<%= contact_detail.label :last_name %>
<%= contact_detail.text_field :last_name %>
</div>
<div class="field">
<%= contact_detail.label :description %>
<%= contact_detail.text_area :description %>
</div>
<div class="field">
<%= contact_detail.label :telephone %>
<%= contact_detail.number_field :telephone %>
</div>
<div class="field">
<%= contact_detail.label :mobile %>
<%= contact_detail.number_field :mobile %>
</div>
<% end %>
</div>
...
But my contact detail fields are not shown. Any ideas?
You have to "seed" the relation in order for the inputs for an association to appear. fields_for works like a loop. If the association is empty or nil the block runs 0 times.
Normally you would do this in the new action of your controller:
class UsersController < ApplicationController
def new
#user = User.new
#user.build_contact_detail
end
end
In Devise the new action is Devise::RegistrationsController#new which you can customize by subclassing the controller:
class MyRegistrationsController < Devise::RegistrationsController
def new
super do |user|
user.build_contact_detail
end
end
end
super do |user| ... end uses the fact that all the Devise controller actions take a block and yield the resource. This makes it really easy to customize them without copy-pasting the entire method.
You then have to alter the routes so that your custom controller is used:
Rails.application.routes.draw do
devise_for :users, controllers: {
registrations: 'my_registrations'
}
end
So I have a User model and a Contract model with a many to many relationship. Contract belongs to multiple users but with different ids so i can do this
Contract.find(params[:id]).creator.email
Contract.find(params[:id]).leader.email
Contract.find(params[:id]).buyer.email
Contract.find(params[:id]).seller.email
User.find(params[:id]).contracts
In my form I have this, so I can set each id to an already created user
<%= simple_form_for(#contract) do |f| %>
<% if #contract.errors.any? %>
<div id="error_explanation">
<h3><%= pluralize(#contract.errors.count, 'error') %> prohibited this contract from being saved:</h3>
<ul>
<% #contract.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.label :creator_id %><br>
<%= f.select(:creator_id, User.all.map { |c| [c.email, c.id] }) %>
</div>
<div class="form-group">
<%= f.label :leader_id %><br>
<%= f.select(:leader_id, User.all.map { |c| [c.email, c.id] }) %>
</div>
<div class="form-group">
<%= f.label :buyer_id %><br>
<%= f.select(:buyer_id, User.all.map { |c| [c.email, c.id] }) %>
</div>
<div class="form-group">
<%= f.label :seller_id %><br>
<%= f.select(:seller_id, User.all.map { |c| [c.email, c.id] }) %>
</div>
<% end %>
My contract is created with each user id correctly set but when I check the user's contracts, I can only find the same contract if user was the seller (so the last div in the form)
Edit:
Here's my two models.
class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :contract, :foreign_key => 'creator_id'
has_many :contract, :foreign_key => 'leader_id'
has_many :contract, :foreign_key => 'buyer_id'
has_many :contract, :foreign_key => 'seller_id'
validates :email, presence: true, uniqueness: true
validates :role, presence: true
end
class Contract < ApplicationRecord
belongs_to :creator, :class_name => 'User', :foreign_key => 'creator_id'
belongs_to :leader, :class_name => 'User', :foreign_key => 'leader_id'
belongs_to :buyer, :class_name => 'User', :foreign_key => 'buyer_id'
belongs_to :seller, :class_name => 'User', :foreign_key => 'seller_id'
end
Contract controller:
class ContractsController < ApplicationController
before_action :set_contract, only: [:show, :edit, :update, :destroy]
def index
#contracts = Contract.all
end
def show
#contract = Contract.find(params[:id])
end
def new
#contract = Contract.new
end
def edit
end
def create
#contract = Contract.new(contract_params)
if #contract.save
redirect_to contracts_path, notice: 'Le contract a été créé avec succès'
else
render :new
end
end
def update
if #contract.update(contract_params)
redirect_to contract_path
else
render :edit
end
end
def destroy
#contract.destroy
redirect_to contracts_path
end
private
def set_contract
#contract = Contract.find(params[:id])
end
def contract_params
params.require(:contract).permit(:creator_id,
:leader_id,
:buyer_id,
:seller_id)
end
end
I need to have each of the selectors updating the chosen user but only the last one is working and I can't figure out how to make it work.
I feel like this approach can work but I'm new in rails and maybe I'm using the wrong method.
I tried a simple has_and_belongs_to_many relationship, creating users like explained here http://guides.rubyonrails.org/form_helpers.html under 'Building Complex Forms' but I lost the distinction between each specific user in the form when I set them with a field-for
Hope I'm clear enough, thanks !
Alright, so you have a couple of problems with your model definition.
Your relationship needs to be plural if you are using has_many, so it'll be has_many :contracts. This is most probably what is causing your problem.
It's not the best idea to have multiple foreign keys to the same table. Instead have one has_many :contracts, foreign_key: 'contractor_id'. But then use a separate variable to define the role of the User. And in Contract you'll have belongs_to :user, :foreign_key => 'contractor_id'. You can then specify custom accessor_methods if you want to call creator, leader, etc
I'm trying to create an update form on Rails, for an object that has a foreignkey to another. However, it throws this error. I'm still very greenhorn with Ruby on Rails and have just been following a video tutorial, so I'm not quite sure how to interpret this. I am current using rails 5.0.0
In travelers_controllers.rb, below line
#prf = update_prof_params["profiles_attributes"]["0"]
throws the error
undefined method `[]' for nil:NilClass
edit.html.erb
<div class="col-md-7 col-md-offset-3 main">
<% provide(:title, "Edit user")%>
<center><h1>Update your profile</h1></center>
<%= form_for(#person) do |f| %>
<%= render 'shared/error_messages' %>
<div class="col-md-12">
<%= render 'layouts/profilefields', f: f %>
<%= f.submit "Save Changes", class: "btn btn-large btn-primary" %>
</div>
<% end %>
</div>
_profilefields.html.erb
<%= f.fields_for :profiles do |prf|%>
<!--
<% if !#profileInfo["avatar"].blank? %>
<%= image_tag #contactInfo.avatar_url(:medium).to_s, :class=>"profilePhoto" %>
<% end %>
<div class="photoPreview">
<i class="fa fa-upload photoUpload"></i>
<p id="uploadClick">Click to Upload</p>
</div>
<%= prf.file_field :avatar, accept: 'image/png,image/gif,image/jpeg, image/jpg', id: 'uploadAvatar' %>
<p class="deletePhoto">Delete</p>
-->
<%= prf.label :about %>
<%= prf.text_field :about, :class => "form-control" %>
<%= prf.label :why %>
<%= prf.text_field :why, :class => "form-control" %>
<%= prf.label :goals %>
<%= prf.text_field :goals, :class => "form-control" %>
<%= prf.hidden_field :traveler_id, value: current_traveler.id %>
<% end %>
travelers_controller.rb
class TravelersController < ApplicationController
def edit
#person = Traveler.find(params[:id])
#profileInfo = Profile.find_or_initialize_by(traveler_id: params[:id])
##profileInfo[:email] = current_traveler.email
#This builds the form
#person.build_profile(#profileInfo.attributes)
end
def show
end
def update
#prf = update_prof_params["profiles_attributes"]["0"]
#prof = Profile.find_or_create_by(traveler_id: current_traveler.id)
if #prof.update_attributes(prf)
flash[:success] = "Profile updated"
redirect_to feed_path
else # Failed. Re-render the page as unsucessful
render :edit
end
end
private
def update_prof_params
params.require(:traveler).permit(profiles_attributes: [:about, :why, :goals,
:traveler_id])
end
end
and the models
class Profile < ApplicationRecord
belongs_to :traveler, foreign_key: "traveler_id"
end
class Traveler < ApplicationRecord
# Include default devise modules. Others available are:
# , :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable
has_one :profile
end
In TravelersController, the method update should be used for update traveler, not profile, so you can use mass-update via nested attribute like this:
def update
#traveler = Traveler.find(params[:id])
if #traveler.update(update_prof_params)
flash[:success] = "Profile updated"
redirect_to feed_path
else # Failed. Re-render the page as unsucessful
render :edit
end
end
So the above allow you to create/update profile which belongs to traveler. Besides, ensure the nested attribute was defined in your model:
traveler.rb
class Traveler < ActiveRecord::Base
# Your code here
#....
# Make sure define this
accepts_nested_attributes_for :profile
end
Update: The permitted params should be:
def update_prof_params
params.require(:traveler).permit(profile_attributes: [:about, :why, :goals, :traveler_id])
end
As you see profile_attributes should be used instead of profiles_attributes because traveler has one profile only
Hi this question is basically the same as this one, which had no responses. I'm trying to combine the Devise registration form to include fields that produce not only a "user", but a "customer" object, an "account" object for that customer, and an "address" for that customer.
When visitor clicks "Sign Up", the registration form should include the standard Devise stuff, but also the fields for the creation of the other objects. Instead, I get this error:
NoMethodError in Registrations#new
undefined method `build_address' for #
Extracted source (around line #6):
<div class="panel panel-default" style="width: 14em;">
<% resource.build_customer if resource.customer.nil? %>
<% resource.build_account if resource.accounts.nil? %>
<% resource.build_address if resource.address.nil? %>
<%= form_for(resource, as: resource_name, url: >registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<h3>User Info</h3>
Rather than explaining all the relationships, here are the models:
user.rb
class User < ActiveRecord::Base
before_create :generate_id
# Virtual attribute for authenticating by either username or email
# This is in addition to a real persisted field like 'username'
attr_accessor :login
has_one :administrator
has_one :customer
has_many :accounts, through: :customer
accepts_nested_attributes_for :customer, :allow_destroy => true
accepts_nested_attributes_for :accounts, :allow_destroy => true
has_one :address, through: :customer
accepts_nested_attributes_for :customer, :allow_destroy => true
accepts_nested_attributes_for :address, :allow_destroy => true
validates_uniqueness_of :email, :case_sensitive => false
validates_uniqueness_of :id
validates :username,
:presence => true,
:uniqueness=> {
:case_sensitive => false
}
# User ID is a generated uuid
include ActiveUUID::UUID
natural_key :user_id, :remember_created_at
belongs_to :user
# specify custom UUID namespace for the natural key
uuid_namespace "1dd74dd0-d116-11e0-99c7-5ac5d975667e"
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :timeoutable, :recoverable, :trackable, :validatable
# Generate a random uuid for new user id creation
def generate_id
self.id = SecureRandom.uuid
end
# Allow signin by either email or username ("lower" function might have to be removed?)
def self.find_for_database_authentication(warden_conditions)
conditions = warden_conditions.dup
if login = conditions.delete(:login)
where(conditions.to_h).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
else
where(conditions.to_h).first
end
end
end
customer.rb
class Customer < ActiveRecord::Base
belongs_to :user
has_one :address
has_many :accounts
validates :phone1, :firstname, :lastname, presence: true
end
account.rb
class Account < ActiveRecord::Base
belongs_to :customer
belongs_to :user
has_one :acct_type
has_many :acct_transactions
validates :acct_type, presence: true
end
address.rb
class Address < ActiveRecord::Base
belongs_to :customer
belongs_to :user
validates :zip_code, presence: true
validates :address1, presence: true
has_one :zip_code
has_one :state, through: :zip_code
end
The two controllers in question:
registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
before_filter :configure_permitted_parameters
# GET /users/sign_up
def new
#user = current_user
#customer = nil ##user.customer
#account = nil ##customer.account
#address = nil ##customer.address
# Override Devise default behavior and create a customer, account, and address as well
build_resource({})
resource.build_customer
respond_with self.resource
build_resource({})
resource.build_account
respond_with self.resource
build_resource({})
resource.build_address
respond_with self.resource
end
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u|
.permit(:username, :email, :password, :password_confirmation,
:customer_attributes => [:phone1, :phone2, :title, :firstname, :lastname],
:account_attributes => :acct_type,
:address_attributes => [:address1, :address2, :zip_code])
}
end
end
addresses_controller.rb (The important parts)
def new
#customer = current_user.customer
#address = #customer.address.build(:customer_id => #customer.id,
:address1 => nil,
:address2 => nil,
:zip_code => nil)
end
def create
#customer = current_user.customer
#address = #customer.address.build(:customer_id => #customer.id,
:address1 => nil,
:address2 => nil,
:zip_code => nil)
respond_to do |format|
if #address.save
format.html { redirect_to #address, notice: 'Address was successfully created.' }
format.json { render :show, status: :created, location: #address }
else
format.html { render :new }
format.json { render json: #address.errors, status: :unprocessable_entity }
end
end
end
And here is the view where the exception is raised (It's really long so actually the important parts):
<h1>Create an account</h1>
<div class="form-group">
<div class="panel panel-default" style="width: 14em;">
<% resource.build_customer if resource.customer.nil? %>
<% resource.build_account if resource.accounts.nil? %>
<% resource.build_address if resource.address.nil? %>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<h3>User Info</h3>
<div class="form-group">
<!-- fields for User object -->
<div class="field">
<%= f.label :username %><br />
<%= f.text_field :username, autofocus: true %>
</div>
...
<!-- fields for Customer object -->
<%= f.fields_for :customer do |customer_fields| %>
<div class="field">
<%= customer_fields.label :firstname %>
<%= customer_fields.text_field :firstname %>
</div>
...
<!-- fields for Account object -->
<%= f.fields_for :account do |account_fields| %>
<div class="field">
<%= account_fields.label :acct_type %>
<%= account_fields.text_field :acct_type %>
</div>
<% end %>
<!-- fields for Address object -->
<%= f.fields_for :address do |address_fields| %>
<div class="field">
<%= address_fields.label :address1 %>
<%= address_fields.text_field :address1 %>
</div>
...
The exception is pointing to the block of statements at the top...
<% resource.build_customer if resource.customer.nil? %>
<% resource.build_account if resource.accounts.nil? %>
<% resource.build_address if resource.address.nil? %>
... which has given me trouble before. Before the current error I was getting a similar error from the second line ("build_account"). But that turned out to be a pluralization issue, which I believe I've fixed. Since the HTML is read sequentially, it would seem that there is no problem with the first two build_ methods. Why is there then a problem with the build_address method?
I need to fix this error before I can know if the whole thing will actually work or not. Any ideas?
Thanks
It's Rails 4.1.8 / Devise 3.4.1
The trouble turned out to be the syntax I was using create multiple resource objects. It would pass one, but ignored the rest. What I ended up doing to make it work (or at least make the error go away) was to override the build_resource method to accept an array of parameters for each object to be instantiated:
def new
#user = current_user
build_resource({})
self.resource[:customer => Customer.new, :account => Account.new, :address => Address.new]
respond_with self.resource
end
def build_resource(hash=nil)
hash ||= params[resource_name] || {}
self.resource = resource_class.new(hash)
end
def create
# Override Devise default behavior and create a customer, account, and address as well
resource = build_resource(params[:user])
if(resource.save)
sign_in(resource_name, resource)
respond_with resource, :location => after_sign_up_path_for(resource)
else
render :action => "new"
end
end
Also, I removed the three lines at the top of the form view as they attempted to do some sort of pre-validation in the view and just caused problems. Plenty of validation will happen when the form is submitted. This seems to be doing something good. Now I'm working with the form view and having trouble getting each part to render. Fields_for is rendering fields for User and Account models, but not Customer or Address.
I want to query multiple tables. For example in posts table there is a user_id linked to users id. While showing every post, I also want to display the picture of the user. My approach is this, but there is a problem. #user.picture method is undefined.
<% #programs.each do |post| %>
<%= #user = User.where("id = post.user_id") %>
<li class="one-third column">
<div class="wrapper">
<div class="postThumb"><img src="<%= #user.picture %>" /></div>
<div class="postDetails">
<%= link_to "#{ post.title.upcase! }".html_safe, all_posts_path, :class => "postTitle" %>
<p><%= truncate post.details, :length => 90 %></p>
</div>
</div>
</li>
<% end %>
Program Controller:
class ProgramController < ApplicationController
def index
#programs = Program.all
end
User model:
class User < ActiveRecord::Base
attr_accessible :password, :username, :oauth_token, :provider, :uid, :oauth_expires_at, :picture, :email, :name, :location, :gender, :updated_at, :is_admin
has_many :posts
has_one :program
has_many :programdetails, :through => :program
end
Program model:
class Program < ActiveRecord::Base
attr_accessible :details, :title, :user_id
belongs_to :user
has_many :programdetails
end
Try this instead, in the controller:
#programs = Program.includes(:user) # this will return all programs and related users in
# 2 database queries rather than N+1 queries
Then in the view:
<div class="postThumb"><img src="<%= post.user.picture %>" /></div>
Also, you can use image_tag instead.
Finally, you can probably change your post title link to:
<%= link_to post.title.upcase, all_posts_path, :class => "postTitle" %>
#user = post.user. Rails association will give back the associated user by itself.
And to correct above, syntactically, #user = User.find(post.user_id)
try changing this: #user = User.where("id = post.user_id")
into this: #user = User.where(id: post.user_id).first
or even better: #user = post.user as suggested by kiddorails
There is a much easier way to do this using the relations you have already defined:
Programs Controller
def index
#programs = Program.
includes(:user). # eager load user relation to avoid n+1
all
end
View
<% #programs.each do |post| %>
<li class="one-third column">
<div class="wrapper">
<div class="postThumb"><img src="<%= post.user.picture %>" /></div>
<div class="postDetails">
<%= link_to "#{ post.title.upcase! }".html_safe, all_posts_path, :class => "postTitle" %>
<p><%= truncate post.details, :length => 90 %></p>
</div>
</div>
</li>
<% end %>