Is it ok to use User.find in a view partial? - ruby-on-rails

I have 3 models
User
Business (acts_as_commentable using acts_as_commentable gem)
Comment (belongs to user)
On the Business show page I display all the comments associated with that business:
Name:<%= #business.name %>
<%= render #business.comments %>
comments partial
Comment: <%= comment.comment %><br >
By: <%= User.find(comment.user_id).name %>
Is there a better way to display the name of the user instead of using User.find?
Here are my models
User
class User < ActiveRecord::Base
attr_accessible :email, :name
end
Business
class Business < ActiveRecord::Base
attr_accessible :name, :category
has_reputation :votes, source: :user, aggregated_by: :sum
acts_as_commentable
end
Comment
class Comment < ActiveRecord::Base
include ActsAsCommentable::Comment
attr_accessible :comment, :user_id
belongs_to :commentable, :polymorphic => true
default_scope :order => 'created_at ASC'
belongs_to :user
validates :user_id, presence: true
end

I believe you can just use this:
<%= comment.user.name %>
Rails will handle loading for you.

Each comment belongs to a user. So, you just have to get the user this way
comment.user.name

I think the best way is:
<%= comment.user.try(:name) %>
I see you have validates :user_id, presence: true to have user. But if the use has deleted, comment.user.name will put the error.
Or you can use:
<%= comment.user.blank? ? "Visitor" : comment.user.name %>

Related

Validation failed: Park must exist, User must exist

I'm working on a project where Users can see dog Parks and make individual Playdates for each park. The issue I'm having is that the PlaydatesController create action is not persisting the user_id and park_id that each new playdate is associated with. I've tried adding optional: true to my Playdate model, which does save each playdate. However, doing this makes a null column entry for the user_id and park_id.
All I need is the user_id and park_id to create a playdate and keep the association between playdates and parks... Did I mess up my associations? Any help is GREATLY appreciated.
Here's my code:
Playdate MODEL:
class Playdate < ApplicationRecord
belongs_to :park
belongs_to :user
validates :date, presence: true
validates :time, presence: true
end
Park MODEL:
class Park < ApplicationRecord
has_many :playdates
has_many :comments
has_many :users, through: :comments
end
User MODEL:
class User < ApplicationRecord
has_many :parks
has_many :playdates
has_many :comments, through: :parks
end
Playdates CONTROLLER:
def create
#playdate = Playdate.new(playdate_params)
if #playdate.save!
redirect_to park_path(#park)
else
render :new
end
end
private
def playdate_params
params.require(:playdate).permit(:time, :date, :user_id, :park_id)
end
Playdates NEW VIEW:
<%= form_for #playdate do |f| %>
<%= f.label :date %>
<%= f.date_field :date %><br><br>
<%= f.label :time %>
<%= f.time_field :time %><br><br>
<%= hidden_field_tag :user_id, current_user.id %>
<%= hidden_field_tag :park_id, #park%>
<%= f.submit "Add Playdate!" %>
<% end %>
You should use:
f.hidden_field :user_id, value: current_user.id
f.hidden_field :park_id, value: #park.id
The rendered HTML is not the same between "hidden_field_tag" and "hidden_field". Try by yourself to see the difference.

Rails create dynamic attributes

I'm trying to make e-commerce shop with RoR. Most of the required functionality I did without any problems, but now I really need somebody's help here.
I want to make product attributes, like a "Size", "Weight", "Color", etc.
Easiest way is to define this attributes in model migration, but now I want to make attributes dynamic. The main problem is that I can't get all params with attributes from forms when trying to create product.
products/new.html.erb
<%= form_for #product, url: admin_products_path(#product) do |f| %>
<%= f.label :name, 'Name' %>
<%= f.text_field :name, class: "form-control" %>
<%= text_field_tag "product[product_feature][]" %>
<%= text_field_tag "product[product_feature][]" %>
<%= f.submit "Submit" %>
<% end %>
So, I want to generate many fields with attribute name and value, fill them and use these params in controller to interate them and finally create product attributes.
Like
params[:product_features].each do |k, v|
ProductFeature.create(name: k, value: v, product_id: product_id)
end
All gems, that can manipulate with dynamic attributes aren't working with Rails 5+, so I need to find solution for this problem.
I even have working simple db solution for this, but it's uncomfortable to create params. Here it is.
Product.rb
class Product < ApplicationRecord
has_many :product_features
has_many :features, :through => :product_features
end
ProductFeature.rb
class ProductFeature < ApplicationRecord
belongs_to :product
belongs_to :feature
end
Feature.rb
class Feature < ApplicationRecord
end
Make a new model, a child of product called ProductAttribute with two attributes.
Class ProductAttribute < ApplicationRecord
belongs_to :product
validates :name, presence: true
validates :value, presence: true
end
Then use cocoon, or just accepts_nested_attributes
class Product < ApplicationRecord
has_many :product_attributes, as: :attributes
accepts_nested_attributes_for :attributes, allow_destroy: true
end
class ProductsController < ApplicationController
.
.
.
private
.
.
def product_params
params.require(:product).permit(. . . attributes_attributes: [:id, :name, :value])
end
end
Cocoon is definitely what you're looking for.
Heres a quick example i found
class Product
belongs_to :collection
end
class Collection
has_many :products
end
and then in your view something like this
<%= collection_select(:product, :collection_id, Collection.all, :id, :name) %>

form validation with nested models in Rails

I have this problem. I need to validate the attributes of two models in the same form in Rails. One is the parent of the other.
The form is like this:
<%= semantic_form_for #professional do |pro| %>
<%= pro.inputs :id => "information" do %>
<%= pro.input :name, label: t("Artistic Name") %>
<%= pro.semantic_fields_for #user do |user| %>
<%= user.inputs :id => "register" do %>
<%= user.input :email, :placeholder=>"email#example.com" %>
<%= user.input :password, label: t('Password') %>
<%end%>
<% end %>
<% end %>
<% end %>
The models I am using are like this:
User:
class User < ActiveRecord::Base
belongs_to :role, polymorphic: true
validates :email, :password, presence: true
end
Professionals:
class Professional < ActiveRecord::Base
has_one :user, as: :role, dependent: :destroy
accepts_nested_attributes_for :user
validates :date_birthday, :gender, :height, :name, :description, :Weight, :address, :languages,:services, :category, :phonenumber, :fullname, :hair_color, :age, :orientation, presence: true
end
So, what is the problem?
When I clicked in the submit button the professional attributes are marked but not the users attributes.
Like this:
The fields marked in red belongs to the professional model but the fields email and password belongs to the user model aren't marked in red when it should be because they are empty.
What can i do? I need the warning message for the user is attributes too
Thanks in advances.
We've achieved what you need before.
We had to use inverse_of so that the object was a singular piece of data (rather than multiple pieces as is the case by default):
#app/models/user.rb
class User < ActiveRecord::Base
belongs_to :role, polymorphic: true, inverse_of: :user
validates :email, :password, presence: true
end
#app/models/professional.rb
class Professional < ActiveRecord::Base
has_one :user, as: :role, dependent: :destroy, inverse_of: :role
accepts_nested_attributes_for :user
end
This will help.
You also need to make sure you're passing these objects correctly (I see so many people not doing this).
You need to tell Professional to validate the associated User:
class Professional < ActiveRecord::Base
...
validates_associated :user

How do I include has_many relationships in a Rails form?

I have two models, Artist and User that are connected through a third model, ArtistMembership.
From the edit/new Artist form, I want to be able to edit the role of any User in an existing ArtistMembership relationship for that Artist, delete ArtistMemberships, and add new AtistMembership relationships, which would include a User and :role.
Here's my Artist model:
class Artist < ActiveRecord::Base
has_many :artist_memberships, foreign_key: "artist_id", dependent: :destroy
attr_accessible :bio, :created_at, :email, :location, :name, :updated_at, :website, :pic
accepts_nested_attributes_for :artist_memberships, :allow_destroy => :true
...
end
Here's my User model:
class User < ActiveRecord::Base
...
has_many :artist_memberships, foreign_key: "user_id"
...
end
Here's my ArtistMembership model:
class ArtistMembership < ActiveRecord::Base
belongs_to :artist, class_name: "Artist"
belongs_to :user, class_name: "User"
attr_accessible :artist_id, :created_at, :role, :user_id
end
If I have a _form.hml.erb too, for editing Artists that starts:
<%= form_for #artist do |artist_form| %>
<div class="field">
<%= artist_form.label :name %>
<%= artist_form.text_field :name %>
</div>
..
<div class="actions">
<%= artist_form.submit %>
</div>
<% end %>
how can I create the related ArtistMembership forms for the aforementioned functionality?
May be this is helpful for you, see this field_for
you can use accepts_nested_attributes_for(*attr_names)
Maybe you are looking for this method.
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for
Refer to the "One-to-many" section.
But if I were you, I would rather use the "Nested Resource" technic.
http://guides.rubyonrails.org/routing.html#nested-resources

Insert more than one item from a belongs_to association using nested forms

in my Rails way, I have just found another question to the commmunity.
I have the following project: I have a calendar which render me the events that any client have dealed with my company. If I want to insert a new event with only one client, I have no problem, but if I want to insert a new event with more than one client, I have a problem.
But it's been impossible to insert more than one client at the same time. I know that with this configuration, Rails only accepts one client for every instance of event, and one solution could be change the association model between Event and Client, but it makes no sense for me (event has_many clients and client belongs_to events... sounds weird). So, this post it's related to compile options from the community.
This is my code (I'm using the nested_form gem by Ryan Bates [github.com/ryanb/nested_form.git]):
UPDATE: By the moment, I introduce a new model Group, in this way:
models/group.rb
class Group < ActiveRecord::Base
has_many :clients
has_many :events
accepts_nested_attributes_for :clients
accepts_nested_attributes_for :events
attr_accessible :events_attributes, :clients_attributes
end
models/client.rb
class Client < ActiveRecord::Base
has_many :events
belongs_to :group
accepts_nested_attributes_for :events
accepts_nested_attributes_for :group, :update_only => true
attr_accessible :name, :surname, :email, :group_attributes, :events_attributes
end
models/event.rb
class Event < ActiveRecord::Base
belongs_to :group
accepts_nested_attributes_for :group, :update_only => true
attr_accessible :title, :group_id, :starts_at, :ends_at, :group_attributes
end
views/events/_form.html.erb
<%= simple_nested_form_for #event do |f| %>
<%= f.input :title %>
<%= f.fields_for :group do |group_form| %>
<%= group_form.fields_for :clients do |client_form| %>
<%= client_form.input :name %>
<%= client_form.input :surname %>
<%= client_form.input :email, :as => :email %>
<%= client_form.link_to_remove "Remove this client" %>
<% end %>
<%= group_form.link_to_add "Add another client", :clients %>
<% end %>
<%= f.input :starts_at, :as => :datetime %>
<%= f.input :ends_at, :as => :datetime %>
<%= f.button :submit %>
<% end %>
But know, my problem is that, when I create an event for one group, this group is successfully create when I insert ONLY ONE client. When I try to insert two or more clients I get the next:
ActiveModel::MassAssignmentSecurity::Error (Can't mass-assign protected attributes: new_1332430522879):
app/controllers/events_controller.rb:44:in `new'
app/controllers/events_controller.rb:44:in `create'
Ah! And I forget to comment before (but I think that is not relevant for that), that I'm using Ruby 1.9.3-p125 and Rails 3.2.2
Any ideas?
Thanks in advance...
Foncho

Resources