во-первых, вы можете проверить все файлы github здесь: ссылка
У меня есть две модели, которые имеют одинаковые столбцы фильтра.User
модель имеет :country id
также Poll
модель имеет :country_id
на странице индекса, мне нужно фильтр-шоу, у них такие же country_id
по current_user
.
мойКонтроллер опросов находится ниже:
class PollsController < ApplicationController
before_action :authenticate_user!
before_action :check_exist
def index
@polls = Poll.all
@person = Person.find_by_poll_id(params[:poll_id])
if @poll.person.country_id == current_user.person.country_id
@polls = Poll.order('created_at DESC')
else
flash[:warning] = 'there is no polls these matching by your countries'
end
end
def show
@poll = Poll.includes(:vote_options).find_by_id(params[:id])
@vote = Vote.new
end
def new
@poll = current_user.polls.build
@person = @poll.build_person
end
def edit
@poll = Poll.find_by_id(params[:id])
@person = Person.find_by_poll_id(params[:poll_id])
end
def update
@poll = Poll.find_by_id(params[:id])
@person = Person.find_by_poll_id(params[:poll_id])
if @poll.update_attributes(poll_params) && @poll.person.update(people_edit_params)
flash[:success] = 'Poll was updated!'
redirect_to polls_path
else
render 'edit'
end
end
def create
@poll = current_user.polls.new(poll_params)
@person = @poll.build_person(people_edit_params)
@person_user_id = nil
if @poll.save
flash[:success] = 'Poll was created!'
redirect_to polls_path
else
render 'new'
end
end
def destroy
@poll = Poll.find_by_id(params[:id])
if @poll.destroy
flash[:success] = 'Poll was destroyed!'
else
flash[:warning] = 'Error destroying poll...'
end
redirect_to polls_path
end
private
def poll_params
params.require(:poll).permit(:topic, vote_options_attributes: [:id, :user_id, :title, :_destroy])
end
def check_exist
if Person.exists?(user_id: current_user.id)
else
flash[:warning] = 'You dont have any person details, please create'
redirect_to people_new_path
end
end
def people_edit_params
params.require(:person).permit(:gender, :birthday, :country_id,:country_name,:state_id, :lang, :id, :poll_id)
end
end
методы создания, обновления, редактирования и показа работают, но индексируются.без условия if и else. Опросы. заказ работает.
обновление: я также добавил файл модели.
файл модели человека:
class Person < ApplicationRecord
enum gender: [:undisclosed, :female, :male, :other]
belongs_to :country
belongs_to :state, optional: true
belongs_to :user, optional: true
belongs_to :poll, optional: true
delegate :country, :to => :poll, :allow_nil => true
end
модель опроса:
class Poll < ApplicationRecord
#attr_accessible :id, :topic
belongs_to :user
has_many :vote_options, dependent: :destroy
accepts_nested_attributes_for :vote_options, :reject_if => :all_blank, :allow_destroy => true
validates :topic, presence: true
has_one :person
accepts_nested_attributes_for :person
def normalized_votes_for(option)
votes_summary == 0 ? 0 : (option.votes.count.to_f / votes_summary) * 100
end
def votes_summary
vote_options.inject(0) {|summary, option| summary + option.votes.count}
end
end
возвращаемая ошибка браузера:
undefined method `person' for nil:NilClass
как я могу отфильтровать опросы согласно current_user.person.country_id?
спасибо.