RSpec + Capybara scoping returns unexpected results - ruby-on-rails

I have two tables: Suggestions and Keywords.
I'm trying to test removal of items from Keywords (which puts it into Suggestions automatically).
Here's my test:
it "removes a chosen keyword" do
page.first(:link, "Add").click
within(:css, "#keywords") do
find('li:nth-child(1)').click_link('X')
expect(page).to have_no_content(item.search.term)
end
end
It results in:
1) New collection Suggestions removes a chosen keyword
Failure/Error: expect(page).to have_no_content(item.search.term)
expected not to find text "Gift for her" in "Keywords\nGift for her X"
# ./spec/features/collections_spec.rb:60:in `block (4 levels) in <top (required)>'
# ./spec/features/collections_spec.rb:53:in `block (3 levels) in <top (required)>'
Here's the print of the body element:
<div class="col-lg-5 col-md-6 mb-4" id="suggestions">
<div class="card h-100">
<div class="card-body">
<h4 class="card-title">Suggestions</h4>
<p class="card-text">
<ul class="list-group list-group-flush" id="suggestion-list">
<li class="list-group-item">
Gift for her
<a class="btn btn-primary" rel="nofollow" data-method="post" href="/collections/1/item/choose.1">Add</a><a class="btn btn-primary" rel="nofollow" data-method="post" href="/collections/1/item/destroy.1">X</a><br>
</li>
</ul>
</p>
</div>
</div>
</div>
<div class="col-lg-5 col-md-6 mb-4" id="keywords">
<div class="card h-100">
<div class="card-body">
<h4 class="card-title">Keywords</h4>
<p class="card-text">
<ul class="list-group list-group-flush" id="keyword-list">
</ul>
</p>
</div>
</div>
</div>
And my show.html.erb file:
<div class="col-lg-5 col-md-6 mb-4" id="suggestions">
<div class="card h-100">
<div class="card-body">
<h4 class="card-title">Suggestions</h4>
<p class="card-text">
<ul class="list-group list-group-flush" id="suggestion-list">
<% if #suggestions %>
<% #suggestions.each do |s| %>
<li class="list-group-item">
<%= Hpricot.uxs s.search.term %>
<%= link_to 'Add', collection_item_choose_path(#collection, s),{ method: :post, class: "btn btn-primary" } %><%= link_to 'X', collection_item_destroy_path(#collection, s),{ method: :post, class: "btn btn-primary" } %><br>
</li>
<% end %>
<% end %>
</ul>
</p>
</div>
</div>
</div>
<div class="col-lg-5 col-md-6 mb-4" id="keywords">
<div class="card h-100">
<div class="card-body">
<h4 class="card-title">Keywords</h4>
<p class="card-text">
<ul class="list-group list-group-flush" id="keyword-list">
<% if #keywords %>
<% #keywords.each do |s| %>
<li class="list-group-item">
<%= Hpricot.uxs s.search.term %>
<%= link_to 'X', collection_item_remove_path(#collection, s),{ method: :post, class: "btn btn-primary" } %><br>
</li>
<% end %>
<% end %>
</ul>
</p>
</div>
</div>
</div>
Seems to me like the scoping is off somehow, although I checked and the link does actually click (you can see it in the puts page.body). Any ideas?
Thanks in advance!

This is unlikely to be a scoping issue since the reported text is "Keywords\nGift for her X" - whereas if it were seeing the text in the "suggestions" section if would have "Add" text in there too.
More likely is the clicking on the 'X' just isn't getting processed by your app due to an error somewhere. Check your test.log to see exactly what methods are being called, and whether there are errors.
Additionally you don't mention what driver you're using with Capybara. If you're using a JS capable driver it's possible you don't have Capybara.default_max_wait_time set high enough for the hardware you're testing on and/or turbolinks (assuming you have that in your app) is getting involved. Again, looking at your test.log should give a better idea of what's actually happening. Another to try would be to use save_and_open_screenshot to get an image of what the actual page looks like (assuming you're using a driver that supports it)
If you're not using a JS capable driver (ie you're using rack_test) then it's possible you haven't required capybara/rails and the method of the links isn't being used. Again, checking test.log will show the method (get vs post) being used for each request.

Related

Targeting css class of one single variable in embedded ruby loop using coffeescript

My apologies for the confusing title. I am not very sure how to describe this question, please suggest alternative titles if possible!
I'm building a web app where you can like an answer, like most social apps. The problem is, I am new to using coffeescript and erb, so I am not sure how to target my jQuery animations to specific elements.
So my html.erb file looks like this. It has a ERB loop where I go through each answer and display it.
<% #question.answers.each do |answer| %>
<div class="row">
<div class="col-lg-2 col-sm-2">
<div class="like">
<button class="heart-btn">
</button>
<div class="liked">
+1
</div>
</div>
<div class="option">
<a href='/'>Report</a>
<br><a href='/'>Edit</a>
<br>
<%= link_to 'Delete', question_answer_path(#question, answer), method: :delete, data: { confirm: 'Are you sure?' } %>
</div>
</div>
<div class="col-lg-10 col-sm-10">
<div class="row" style="margin:0">
<!-- USER PROFILE -->
<a href='/users/2/profile'>
<div class="mini-profile">
<%= image_tag(#tutor.picture_url) %>
<ul style="list-style-type:none">
<li class="username"><%= #tutor.username %></li>
<li><i class="fa fa-flag" style="padding: 2px"></i><%= #tutor.country %></li>
<li><i class="fa fa-graduation-cap"></i><%= #tutor.educational_level %></li>
</ul>
</div>
</a>
</div>
<div class="row">
<div class="ans-box">
<ul style="list-style-type:none">
<li><strong>This answer was written on:</strong> <%= answer.created_at %></li>
<li class="ans-content"><strong>This is my answer:</strong> <%= raw answer.answercontent %></li>
</ul>
</div>
</div>
</div>
</div>
<hr class="line">
<% end %>
Now, for the div of class 'like', I have the following js written (document ready omitted):
# Heart like
$(".heart-btn").click ->
$(this).toggleClass('clicked')
$(".liked").show().fadeOut(500)
the 'liked' class is the +1 that will show when the heart is clicked.
But as you can probably imagine, this makes ALL the hearts on the page display the '+1'. I want to target only the 'heart' of the 'answer' that was clicked. I reckon I can do this by referencing the current 'answer' as a dom object, but I am unsure on how to do this. My attempt is doing something like:
# Heart like
current_ans = <%= answer %>
$(".heart-btn").click ->
$(this).toggleClass('clicked')
current_ans.$(".liked").show().fadeOut(500)
But the syntax seems very off to me and it doesn't seem to work.
(I think partially I'm not finding the solution because I cannot phrase my question so well. Please advise if this is the case. Thanks!)
jQuery closest is ideal for this:
$(".heart-btn").click ->
$(this).toggleClass('clicked')
$(this).closest('.like').find(".liked").show().fadeOut(500)

I can only edit my first post

I'm currently facing a strange problem. I'm trying to implement the basics actions into an ajax window. And my problem is that when I'm trying to edit a ranch(like a post), it's always rendering the edit of my first ranch.
This is my code :
(ranch controller)
before_action :set_ranch, only: [:show, :edit, :update, :destroy]
def index
#ranches = current_user.ranches
end
private
# Use callbacks to share common setup or constraints between actions.
def set_ranch
#ranch = Ranch.find(params[:id])
end
(ranch/index)
<%- #ranches.each do |ranch| %>
<div class="box panel panel-default">
<h2><%=link_to ranch.name, ranch%></h2>
<div class="image_wrapper">
<%= link_to (image_tag "ranch.jpg"), ranch %>
</div>
<button type='button' class='btn btn-default' data-toggle='modal' data-target='#MyEditRanch' aria-hidden="true" remote="true" style="float:right; margin-top: 10px;">
<i class="fa fa-cog"></i>
</button>
</div>
<!-- modal Edit -->
<div id='MyEditRanch' class='modal fade' role='dialog' aria-hidden="true">
<div class="modal-dialog">
<div class='content'>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 class="modal-title">Edit <%=ranch.name%></h3>
</div>
<div class="modal-body">
<%= render 'form2', ranch: ranch%>
</div>
</div>
</div>
</div>
<!-- /modal Edit -->
<%end%>
( & my _form2)
<%= simple_form_for(ranch) do |f| %>
<%= f.error_notification %>
<% if ranch.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(ranch.errors.count, "error") %> prohibited this ranch from being saved:</h2>
<ul>
<% ranch.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="col-lg-6">
<div class="input-group" style=" margin-left: 70px;">
<div class="input" style="width: 400px; margin-top: -10px; text-align: center;" >
<%= f.input :name, label: false, style:"margin:0px;", placeholder: "Name of your ranch"%>
</div><!-- /btn-group -->
</div><!-- /input-group -->
<div class="form-actions" style="max-width: 300px">
<%= f.button :submit, style:"text-align:center; margin-left: 70px;" %>
</div>
<%= link_to "Delete", ranch_path(ranch), method: :delete, data: {confirm: "Are you sure that you want to delete this ranch ?"}, class: "btn btn-danger", style:"margin-left: 140px;" %>
<% end %>
</div>
So if you have any advice to solve this shit, you're welcome !!
Thanks in advance
In HTML, ids must be unique on the page.
ie there must be only one of them...
This means there must be only one div that has the id MyEditRanch...
Sometimes the browser lets you get away with it... but if you have JS that uses that id (eg to toggle a modal)... it will still go looking for the first instance of that id on the page to toggle... ie onlyt he first one.
If you want to display multiple ranches - each with their own edit modal... then you really must give each one its own unique id to toggle.
eg append the ranch's id to it in both the button and the edit-modal. eg
<button type='button' data-toggle='modal' data-target='#edit-ranch-<%= ranch.id %>' ...
<!-- and later -->
<div id='#edit-ranch-<%= ranch.id %>'

Bootstrap does not work as it should on my Rails App

I'm trying to create a two column layout. The first side should span 9 columns and the next one 3. Inside the col-md-9 I want to nest two more columns, one that spans 4 and another one that spans 8.
<div class="row">
<% #huddles.each do |huddle| %>
<div class="col-md-9">
<div class="row">
<div class="col-md-4">
<img class="img-responsive" src="http://placehold.it/300x150">
</div>
<div class="col-md-8">
<h4><%= huddle.title %></h4>
<h4 class="huddle-description"><%= huddle.description %></h4>
<%= link_to "Read More...", huddle_path(huddle) %>
</div>
</div>
<% end %>
</div>
<div class="col-md-3">Second Column</div>
</div>
This however, comes out looking like this:
Am I nesting my rows and columns wrong? Or maybe it is my ruby code itself that screws up the layout once new "Huddles" are created?
EDIT: With the fixed code, the second column "col-md-3" comes out next to the last created huddle. Inspecting it, all the huddles make one single row.
<div class="row">
<% #huddles.each do |huddle| %>
<div class="col-md-9">
<div class="row">
<div class="col-md-4">
<img class="img-responsive" src="http://placehold.it/300x150">
</div>
<div class="col-md-8">
<h4><%= huddle.title %></h4>
<h4 class="huddle-description"><%= huddle.description %></h4>
<%= link_to "Read More...", huddle_path(huddle) %>
</div>
</div>
</div>
<% end %>
<div class="col-md-3">Second Column</div>
</div>
And looks like this, where the second column moves all the way down next to the last huddle created:
I think this is what you are going for. I am pretty confident the issue is with the html structure and has nothing to do with ruby.
I also want to clarify. The initial issue you were having was that you were starting a div inside the loop but only closing it outside the loop. So starting n amount of divs but only one closing tag, and that would cause it to look like the image you first posted.
<div class="row">
<div class="col-md-9">
<% #huddles.each do |huddle| %>
<div class="show-region" >
<div class="col-md-4" style="height:150px;">
<img class="img-responsive" src="http://placehold.it/300x150">
</div>
<div class="col-md-8" style="height:150px;">
<h4><%= huddle.title %></h4>
<h4 class="huddle-description"><%= huddle.description %></h4>
<%= link_to "Read More...", subjects_path(huddle) %>
</div>
</div>
<% end %>
</div>
<div class="col-md-3">Second Column</div>
</div>

Rails 4 - dynamically populate bootstrap tabs

I am trying to make a set of tab content fields in my Rails 4 app.
I have a problem because I want the first tab to be displayed (and then the others display as you click through them).
I have 6 tab placeholders, which might be used, depending on whether an attribute is marked as true or false. If one attribute is false, then there is one fewer tab.
If the content that is lined up in the first tab belongs to the attribute which is false, I can't figure out how to make the first tab display (without needing to first click on it).
For example, the first to tabs are below:
<div class="dp-tab-1">
<ul class="dp-tab-list row" id="myTab">
<% if #project.package.has_background_ip == true %>
<li class="col-md-2 col-xs-6 active" >
<a href="#tab-content-first">
<span class="glyph-item" data-icon=""></span>
<span>BACKGROUND INTELLECTUAL PROPERTY</span>
</a>
</li>
<% end %>
<li class="col-md-2 col-xs-6">
<a href="#tab-content-second">
<div class="glyph-item" data-icon=""></div>
<span>DATA</span>
</a>
</li>
The content for those two tabs is:
<div class="dp-tab-content tab-content">
<div class="tab-pane row fade in active" id="tab-content-first">
<% if #project.package.has_background_ip == true %>
<div class="col-md-6 text-center">
<%= image_tag(#project.package.background_ip.bip_image, :class=>"wow fadeInLeft img-responsive", :alt=>"123") %>
</div>
<div class="col-md-6">
<div class="tab-inner">
<h4>EXISTING INTELLECTUAL PROPERTY</h4>
<p class='medium-text'>
<%= render 'projects/bipparticulars' %>
</p>
<br/>
<a class='btn btn-danger btn-line'>CHECK THESE IP TERMS</a>
</div>
</div>
<% end %>
</div>
<div class="tab-pane row fade" id="tab-content-second">
<div class="col-md-6">
<div class="tab-inner">
<h4>DATA</h4>
<p class='medium-text'>
<%= render 'projects/dataparticulars' %>
</p>
<br/>
<% if #project.package.datum.survey_link == true %>
<a class='btn btn-danger btn-line'>SURVEY</a>
<% end %>
<% if #project.package.datum.confidential == true %>
<a class='btn btn-danger btn-line'>PROPOSED NDA</a>
<% end %>
</div>
</div>
<div class="col-md-6">
<%= image_tag(#project.package.background_ip.bip_image, :class=>"wow fadeInUp img-responsive", :alt=>"123") %>
</div>
</div>
The first tab has:
<% if #project.package.has_background_ip == true %>
If that attribute is true, then the tab displays and everything works fine.
If that attribute is not true, then the tab does not display and everything else works fine, EXCEPT that the second tab does not show its content automatically - i first have to click on the tab to reveal its content.
I want the first true tab to have its contents on display.
How can I achieve that?
You should set active class for the second tab to be selected automatically in order to show content :
<div class="dp-tab-content tab-content">
<div class="tab-pane row fade in <%= project.package.has_background_ip == true ? 'active' : '' %>" id="tab-content-first">
<% if #project.package.has_background_ip == true %>
<div class="col-md-6 text-center">
<%= image_tag(#project.package.background_ip.bip_image, :class=>"wow fadeInLeft img-responsive", :alt=>"123") %>
</div>
<div class="col-md-6">
<div class="tab-inner">
<h4>EXISTING INTELLECTUAL PROPERTY</h4>
<p class='medium-text'>
<%= render 'projects/bipparticulars' %>
</p>
<br/>
<a class='btn btn-danger btn-line'>CHECK THESE IP TERMS</a>
</div>
</div>
<% end %>
</div>
<div class="tab-pane row fade <%= project.package.has_background_ip == false ? 'active' : '' %>" id="tab-content-second">
<div class="col-md-6">
<div class="tab-inner">
<h4>DATA</h4>
<p class='medium-text'>
<%= render 'projects/dataparticulars' %>
</p>
<br/>
<% if #project.package.datum.survey_link == true %>
<a class='btn btn-danger btn-line'>SURVEY</a>
<% end %>
<% if #project.package.datum.confidential == true %>
<a class='btn btn-danger btn-line'>PROPOSED NDA</a>
<% end %>
</div>
</div>
<div class="col-md-6">
<%= image_tag(#project.package.background_ip.bip_image, :class=>"wow fadeInUp img-responsive", :alt=>"123") %>
</div>
</div>
Hopefully! It will help.

rails - undefined method `upletter' for nil:NilClass

I want to iterate that cards I create in show
Here is the code in index :
<div class="container-page">
<div class="padding-page">
<div class="container-fluid" id="start-cards">
<div class="row">
<h1 class="text-center">Let's</h1>
<ul class="list-inline text-center">
<% #hiraganas.each do |hiragana| %>
<li>
<div class="col-sm-3 col-xs-4 col-md-3 container-cards">
<div class="card-details">
<span class="card-question img-popover" data-content="<h4 class='text-center letter-uppercase'><%= #hiragana.upletter %></h4><p class='text-center'><%= #hiragana.transcription %></p>"><i class="fa fa-eye fa-lg"></i></span>
<div class="prononciation"><i class="fa fa-comment"></i></div>
<div class="audioclick">
<p><i class="fa fa-volume-off fa-lg"><%= #hiragana.audioclick %></i></p>
</div>
<div class="card-hiragana hiragana-<%=#hiragana.upletter.downcase.last%>">
<p><%= #hiragana.ideoone %></p>
</div>
<div class="card-katakana">
<p><%= #hiragana.ideotwo %></p>
</div>
</div>
</div>
</li>
<% end %>
</ul>
</div>
</div>
</div>
</div>
I have a problem : better errors tells me [![enter image description here][2]][2] undefined method with 'upletter' which is the method I use to generate letter(s) in popover.
<% #hiraganas.each do |hiragana| %>
<li>
<div class="col-sm-3 col-xs-4 col-md-3 container-cards">
<div class="card-details">
<span class="card-question img-popover" data-content="<h4 class='text-center letter-uppercase'><%= #hiragana.upletter %></h4><p class='text-center'><%= #hiragana.transcription %></p>"><i class="fa fa-eye fa-lg"></i></span>
<div class="prononciation"><i class="fa fa-comment"></i></div>
<div class="audioclick">
<p><i class="fa fa-volume-off fa-lg"><%= #hiragana.audioclick %></i></p>
</div>
Bonjour!
You need:
<%= hiragana.upletter %>
<%= hiragana.transcription %>
In your loop.
-
When using a loop, you need to use the locally scoped variable:
<% #hiraganas.each do |hiragana| %>
<%= hirgana.upletter %>
<% end %>
Any Ruby error with undefined method "..." for "NilClass" basically means you're trying to call methods on a non-declared variable.
In this case, the variable you're trying to use is #hiragana, which doesn't exist. It's local equivalent (hirgana) does.

Resources