Rails nested resource is showing up twice.. once at the start and once at the end
up vote
0
down vote
favorite
I have a resource called a ClinicalCase which has_many questions (nested resource). The premise is to show a case all the time, and be able to see one question at a time. My issue is that, if a case has 5 questions, the app is only showing 4. It will show the same question for the first array index and last array index, and everything in between will be the same.
For example, if case 1 has questions "foo", "bar" and "baz", "foo" and "bar" will only show up, with "foo" at the start and the end. I have no idea why this is happening.
I'm using will_paginate to assist with the pagination, as each user needs to see one case at a time, and one question at a time that belongs to the case.
Here's my code (working fine, except that the first and last question of a case show up as the exact same)
controller
def tagged
@cases = ClinicalCase.with_tag(params[:tag]).paginate(:page => params[:case], :per_page => 1)
end
model
class ClinicalCase < ApplicationRecord
belongs_to :user
has_many :questions, dependent: :destroy, inverse_of: :clinical_case
has_many :answers
accepts_nested_attributes_for :questions, allow_destroy: true, reject_if: :all_blank
acts_as_taggable
scope :with_tag, -> (tag) tagged_with(tag) if tag.present?
def paginated_questions(page, per_page = 1)
questions.paginate(page: page, per_page: per_page)
end
view part 1: tagged.html.erb
<% @cases.each do |clin_case| %>
<%= render "clinical_cases/question", clin_case: clin_case, questions: clin_case.questions %>
<%= will_paginate @cases, :param_name => 'case', :previous_label => 'Previous Case', :next_label => 'Next Case', :page_links => false %><br>
<% end %>
view part 2: _question.html.erb
<% questions = clin_case.paginated_questions(params[:page]) %>
<% questions.each do |question| %>
<%= question.title %>
<% question.answers.shuffle.each do |x| %>
<% if x.correct? %>
<%= x.choice %>
<% else %>
<%= x.choice %>
<% end %>
<% end %>
<%= will_paginate questions, :previous_label => 'Prev. Ques.', :next_label => 'Next Question', :page_links => false %>
<% end %>
ruby-on-rails
add a comment |
up vote
0
down vote
favorite
I have a resource called a ClinicalCase which has_many questions (nested resource). The premise is to show a case all the time, and be able to see one question at a time. My issue is that, if a case has 5 questions, the app is only showing 4. It will show the same question for the first array index and last array index, and everything in between will be the same.
For example, if case 1 has questions "foo", "bar" and "baz", "foo" and "bar" will only show up, with "foo" at the start and the end. I have no idea why this is happening.
I'm using will_paginate to assist with the pagination, as each user needs to see one case at a time, and one question at a time that belongs to the case.
Here's my code (working fine, except that the first and last question of a case show up as the exact same)
controller
def tagged
@cases = ClinicalCase.with_tag(params[:tag]).paginate(:page => params[:case], :per_page => 1)
end
model
class ClinicalCase < ApplicationRecord
belongs_to :user
has_many :questions, dependent: :destroy, inverse_of: :clinical_case
has_many :answers
accepts_nested_attributes_for :questions, allow_destroy: true, reject_if: :all_blank
acts_as_taggable
scope :with_tag, -> (tag) tagged_with(tag) if tag.present?
def paginated_questions(page, per_page = 1)
questions.paginate(page: page, per_page: per_page)
end
view part 1: tagged.html.erb
<% @cases.each do |clin_case| %>
<%= render "clinical_cases/question", clin_case: clin_case, questions: clin_case.questions %>
<%= will_paginate @cases, :param_name => 'case', :previous_label => 'Previous Case', :next_label => 'Next Case', :page_links => false %><br>
<% end %>
view part 2: _question.html.erb
<% questions = clin_case.paginated_questions(params[:page]) %>
<% questions.each do |question| %>
<%= question.title %>
<% question.answers.shuffle.each do |x| %>
<% if x.correct? %>
<%= x.choice %>
<% else %>
<%= x.choice %>
<% end %>
<% end %>
<%= will_paginate questions, :previous_label => 'Prev. Ques.', :next_label => 'Next Question', :page_links => false %>
<% end %>
ruby-on-rails
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a resource called a ClinicalCase which has_many questions (nested resource). The premise is to show a case all the time, and be able to see one question at a time. My issue is that, if a case has 5 questions, the app is only showing 4. It will show the same question for the first array index and last array index, and everything in between will be the same.
For example, if case 1 has questions "foo", "bar" and "baz", "foo" and "bar" will only show up, with "foo" at the start and the end. I have no idea why this is happening.
I'm using will_paginate to assist with the pagination, as each user needs to see one case at a time, and one question at a time that belongs to the case.
Here's my code (working fine, except that the first and last question of a case show up as the exact same)
controller
def tagged
@cases = ClinicalCase.with_tag(params[:tag]).paginate(:page => params[:case], :per_page => 1)
end
model
class ClinicalCase < ApplicationRecord
belongs_to :user
has_many :questions, dependent: :destroy, inverse_of: :clinical_case
has_many :answers
accepts_nested_attributes_for :questions, allow_destroy: true, reject_if: :all_blank
acts_as_taggable
scope :with_tag, -> (tag) tagged_with(tag) if tag.present?
def paginated_questions(page, per_page = 1)
questions.paginate(page: page, per_page: per_page)
end
view part 1: tagged.html.erb
<% @cases.each do |clin_case| %>
<%= render "clinical_cases/question", clin_case: clin_case, questions: clin_case.questions %>
<%= will_paginate @cases, :param_name => 'case', :previous_label => 'Previous Case', :next_label => 'Next Case', :page_links => false %><br>
<% end %>
view part 2: _question.html.erb
<% questions = clin_case.paginated_questions(params[:page]) %>
<% questions.each do |question| %>
<%= question.title %>
<% question.answers.shuffle.each do |x| %>
<% if x.correct? %>
<%= x.choice %>
<% else %>
<%= x.choice %>
<% end %>
<% end %>
<%= will_paginate questions, :previous_label => 'Prev. Ques.', :next_label => 'Next Question', :page_links => false %>
<% end %>
ruby-on-rails
I have a resource called a ClinicalCase which has_many questions (nested resource). The premise is to show a case all the time, and be able to see one question at a time. My issue is that, if a case has 5 questions, the app is only showing 4. It will show the same question for the first array index and last array index, and everything in between will be the same.
For example, if case 1 has questions "foo", "bar" and "baz", "foo" and "bar" will only show up, with "foo" at the start and the end. I have no idea why this is happening.
I'm using will_paginate to assist with the pagination, as each user needs to see one case at a time, and one question at a time that belongs to the case.
Here's my code (working fine, except that the first and last question of a case show up as the exact same)
controller
def tagged
@cases = ClinicalCase.with_tag(params[:tag]).paginate(:page => params[:case], :per_page => 1)
end
model
class ClinicalCase < ApplicationRecord
belongs_to :user
has_many :questions, dependent: :destroy, inverse_of: :clinical_case
has_many :answers
accepts_nested_attributes_for :questions, allow_destroy: true, reject_if: :all_blank
acts_as_taggable
scope :with_tag, -> (tag) tagged_with(tag) if tag.present?
def paginated_questions(page, per_page = 1)
questions.paginate(page: page, per_page: per_page)
end
view part 1: tagged.html.erb
<% @cases.each do |clin_case| %>
<%= render "clinical_cases/question", clin_case: clin_case, questions: clin_case.questions %>
<%= will_paginate @cases, :param_name => 'case', :previous_label => 'Previous Case', :next_label => 'Next Case', :page_links => false %><br>
<% end %>
view part 2: _question.html.erb
<% questions = clin_case.paginated_questions(params[:page]) %>
<% questions.each do |question| %>
<%= question.title %>
<% question.answers.shuffle.each do |x| %>
<% if x.correct? %>
<%= x.choice %>
<% else %>
<%= x.choice %>
<% end %>
<% end %>
<%= will_paginate questions, :previous_label => 'Prev. Ques.', :next_label => 'Next Question', :page_links => false %>
<% end %>
ruby-on-rails
ruby-on-rails
edited Nov 8 at 21:51
asked Nov 8 at 21:25
mazing
133114
133114
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
I figured it out, and I'm not sure why it started working but it did.
<% @questions = clin_case.questions.order("created_at ASC").paginate(:page => params[:question], :per_page => 1) %>
<%= render @questions %>
I used the local variable to get the association and added an order method in ASC, and that caused it to work.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I figured it out, and I'm not sure why it started working but it did.
<% @questions = clin_case.questions.order("created_at ASC").paginate(:page => params[:question], :per_page => 1) %>
<%= render @questions %>
I used the local variable to get the association and added an order method in ASC, and that caused it to work.
add a comment |
up vote
0
down vote
I figured it out, and I'm not sure why it started working but it did.
<% @questions = clin_case.questions.order("created_at ASC").paginate(:page => params[:question], :per_page => 1) %>
<%= render @questions %>
I used the local variable to get the association and added an order method in ASC, and that caused it to work.
add a comment |
up vote
0
down vote
up vote
0
down vote
I figured it out, and I'm not sure why it started working but it did.
<% @questions = clin_case.questions.order("created_at ASC").paginate(:page => params[:question], :per_page => 1) %>
<%= render @questions %>
I used the local variable to get the association and added an order method in ASC, and that caused it to work.
I figured it out, and I'm not sure why it started working but it did.
<% @questions = clin_case.questions.order("created_at ASC").paginate(:page => params[:question], :per_page => 1) %>
<%= render @questions %>
I used the local variable to get the association and added an order method in ASC, and that caused it to work.
answered Nov 9 at 11:02
mazing
133114
133114
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53216362%2frails-nested-resource-is-showing-up-twice-once-at-the-start-and-once-at-the-en%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown