Using NestedSortable with Ancestry gem and Mongoid - ruby-on-rails-3

trying to incorporate nested sortable into my rails app.
I'm using Mongoid and the Ancestry gem, and I'm trying to make a page that updates the parent_id of a category on drag and drop with Nested-Sortable.
Been trying to experiment, but don't really understand what NestedSortable spits out in order to create a controller that will read it.
My controller has:
def sort
Category.update_all({parent_id: params[:parent_id].to_i})
render nothing: true
end
Routes:
resources :symbols, :as => :categories, :controller => :categories do
collection {post :sort}
end
Reorder.js.coffee:
jQuery ->
$('.reorder-tree').nestedSortable
handle: '.handle'
items: 'li'
toleranceElement: '> div'
update: ->
$.post($(this).data('update-url'), $(this).nestedSortable('serialize'))
And my reorder view spits out something like:
<ol class=reorder-tree>
<li id="category_513d372b10188f9b6b000014"><div>
<i class="icon-move handle"></i>
Chickne
<span class="btn-group show-on-hover">
<a href="/symbols/new?parent_id=chickne" class="btn btn-micro" data-remote="true" title="Add Sub-Category"><i class="icon-plus"></i>
</a>
<a href="/symbols/chickne/edit" class="btn btn-micro"><i class="icon-pencil"></i>
</a>
<a href="/symbols/chickne" class="btn btn-micro" data-method="delete" data-remote="true" rel="nofollow" title="Delete Category"><i class="icon-remove"></i>
</a>
</span>
</div>
<ol class="visible"><li id="category_513d373310188f9b6b000016"><div>
<i class="icon-move handle"></i>
Smoking Babies
<span class="btn-group show-on-hover">
<a href="/symbols/new?parent_id=smoking-babies" class="btn btn-micro" data-remote="true" title="Add Sub-Category"><i class="icon-plus"></i>
</a>
<a href="/symbols/smoking-babies/edit" class="btn btn-micro"><i class="icon-pencil"></i>
</a>
<a href="/symbols/smoking-babies" class="btn btn-micro" data-method="delete" data-remote="true" rel="nofollow" title="Delete Category"><i class="icon-remove"></i>
</a>
</span>
</div>
<ol class="hide"></ol>
</li>
</ol></li>
</ol>
Any help would be appreciated, thanks.

After seriously playing around with the array I was getting from Nested Sortable,
I was able to make this work by using this in my controller:
def sort
# html = "Env: #{Rails.env}. "
# infor = params[:category]
params[:category].each do |id, attr|
thisCat = params[:category][id]
#category = Category.where(:_id => id).first
# html << "ID= #{id} , thisCat= #{thisCat} Name= #{#category.name} Parent= #{#category.parent_id} ; "
unless thisCat == "null"
#category.parent_id = thisCat.to_s
#category.save
end
end
# flash[:alert] = html
# flash[:error] = infor
# render nothing: true
end
You can see what I commented out after I got it to work; that's all my debugging flash statements.
Cheers!

Related

Soup parser not able to extract class information

While running query:
soup.find_all('div')
I get results:
<div class="class-link">
<a class="btn btn-primary" href="abc" style="text-decoration: underline">
See all</a>
</div>, <div class="sota-table-link">
<a class="btn btn-primary" href="abc" style="text-decoration: underline">
See all</a>
</div>, <div class="class-link">
Though when I run: soup.find_all('div', _class='class-link') I get empty list.
What causes this issue and how can I get correct div?
Pass the class as key-value pairs to the attrs parameter, instead, like this:
soup.find_all('div', {'class': 'class-link'})
Result:
[<div class="class-link">
<a class="btn btn-primary" href="abc" style="text-decoration: underline">
See all</a>
</div>, <div class="class-link"></div>]
According to the BeautifulSoup4 documentation
it must be class_ and not _class
So you code mus be changed to
print(soup.find_all("div", class_="class-link"))
Personally I find css selectors a lot cleaner
soup.select('div.class-link')
where the . is a css class selector

rails 3.2 will paginate change style current page

I am trying to add some style to the current page to highlight it from the other pages style. But my code does not seem to work... all the numbers look the same!
<section id="pagination">
<nav>
<ul>
<li><a class='<%= "active" if (params[:page]).to_i == #myths.current_page %>'>
<!-- checking current page and params page -->
<%= #myths.current_page.to_i %> <br> <%= (params[:page]).to_i %> <br>
<%= will_paginate #myths, :inner_window => 1, :outer_window => 1, :previous_label => '← previous', :next_label => 'next →' %>
</a></li>
</ul>
</nav>
</section>
Any help is appreciated.
update
CSS
a.active{text-decoration:underline;}
Picture of the pagination the current page is 3 (the style is applied when i check for the current page).
I am not using any helper.
Here is the generated html
<section id="pagination">
<nav>
<ul>
<li><a class='active'>
3 <br> 3 <br>
<div class="pagination">
<ul>
<li class="prev previous_page ">
<a rel="prev" href="/tags/Justice?page=2">← previous</a>
</li>
<li><a rel="start" href="/tags/Justice?page=1">1</a></li>
<li><a rel="prev" href="/tags/Justice?page=2">2</a></li>
<li class="active">3</li>
<li><a rel="next" href="/tags/Justice?page=4">4</a></li>
<li>5</li>
<li class="next next_page ">
<a rel="next" href="/tags/Justice?page=4">next →</a>
</li>
</ul>
</div>
</a></li>
</nav>
</section>
this is the problem
<li class="active">3</li>
it should be
<li><a class="active" href="/tags/Justice?page=3">3</a></li>
for it to work. how to fix that?
since i am on page 3, 3 should be underlined the pagination should be like : <- previous 1 2 3 4 5 next ->
Consider reading the following wiki page. Just simplify your view:
<section id="pagination">
<%= will_paginate #myths, :inner_window => 1, :outer_window => 1, :previous_label =>
</section>
To add some special look and feel for the current page link, just set css style for the .current class. will_paginate will do the remaining staff for you and automatically add .current class to the current page link.
Update:
To make your particular case working, just change the css style to reflect the markup
li.active a {
text-decoration:underline;
}

Twitter Bootstrap Carousel disappears only on edit pages

im following this tutorial : http://untame.net/2012/08/twitter-bootstrap-build-a-stunning-two-column-blog/
and my carousel works fine (scrolling works fine, all the images are rendered) but when i navigate to an EDIT page then is just disappears but still shows the scrolling function
here it is working :
http://s7.postimage.org/dsf75yc6z/Carousel.png
and now i pressed EDIT : http://s13.postimage.org/64i1v0hif/Carousel2.png
this is the carousel code in my layout:
<div id="myCarousel" class="carousel slide">
<!-- Carousel items -->
<div class="carousel-inner">
<div class="active item"><img src="../slider/musteri.jpg" /></div>
<div class="item"><img src="../slider/Deepak.jpg" /></div>
<div class="item"><img src="../slider/4.jpg" /></div>
<div class="item"><img src="../slider/1.jpg" /></div>
<div class="item"><img src="../slider/2.jpg" /></div>
</div>
<!-- Carousel nav -->
<a class="carousel-control left" href="#myCarousel" data-slide="prev">‹</a>
<a class="carousel-control right" href="#myCarousel" data-slide="next">›</a>
</div>
my EDIT function is scaffold made:
# GET /news/1/edit
def edit
#news = News.find(params[:id])
respond_to do |format|
format.html # new.html.erb
format.json { render json: #news }
end
end
My EDIT view simply renders <%= render 'form' %>
that form is the same as the one that is used for NEW and there the carousel works fine!
and that is making me nuts! Any ideas ? :D
The issue here is the path to your images. Rails has the asset pipeline so you don't really need to put the images within another folder within the asset folder. (hope that makes sense)
Take your images out of your slider folder and just place them in
assets/images
and then when you want to call an image just use
<img src="/assets/filename.jpg">
Hope that helps

Rails 3 Submit Tag + html_safe

What's wrong with this line of code?
<%= submit_tag "Delete <i class='icon-check'></i>".html_safe, :disable_with => "Deleting", :class => "btn btn-danger"%>
This literally produces:
Evidently my html_safe call isn't doing anything.
Background:
I'm using Twitter Bootstrap as well as Font Awesome and I'm essentially trying to achieve a submit button with an icon inside of it.
To extend on Lukas' answer I needed a button tag rather than an input. This code produced the effect I was looking for:
<button type="submit" class="btn btn-danger">
Delete <i class="icon-check"></i>
</button>
Which resulted in:
I found the answer I was looking for here.
What's wrong with it? Submit button values should not contain embedded HTML code.
This is how submit button looks in HTML:
<input type="submit" value="Submit" />
HTML tags in value attributes are interpreted as text, not as HTML:
<input type="submit" value="<i>Submit</i>" />
<%= form.button :submit, class: 'btn btn-success' do %>
<i class="fa fa-plus"></i> Add Funder <i class="fa fa-chevron-right"></i>
<% end %>
This is good answer.

I get an empty page with capybara visit method, but response object has content

Ok, so I'm working with capybara + rails 3 + rspect
I'm trying to login to my site, but even though I don't get an error when I use the fill_in method, I get an error with the click_on method, since it can't find the element I'm trying to click.
Here is my HTML code:
<form accept-charset="UTF-8" action="/" class="filter_form" id="login" method="post">
<fieldset>
<div class="modal-body">
<div class="clearfix login-fields">
<label for="user_email">Email</label>
<div class="input login-inputs">
<input class="input-text" id="user_email" name="user[email]" placeholder="email" size="30" type="email" value="">
</div>
</div>
<div class="clearfix login-fields">
<label for="user_password">Password</label>
<div class="input login-inputs">
<input class="input-text" id="user_password" name="user[password]" placeholder="password" size="30" type="password">
</div>
</div>
</div>
<div class="modal-footer">
<input class="btn btn-primary login_btn" id="btn_login" name="commit" type="submit" value="Sign in">
Forgot password...
Cancel
</div>
</fieldset>
</form>
And here is the test:
describe "Login ", js: true, type: :request do
it "should login correctly if the right credentials are given" do
Capybara.default_wait_time = 15
Capybara.ignore_hidden_elements = false
visit new_user_session_path
fill_in 'user_email', :with => 'example#test.com'
fill_in 'user_password', :with => 'pwd123'
p page.body
click_on 'Sign in'
visit '/'
save_and_open_page
response.should have_content('Welcome')
end
end
The worst part is that when I do p page.body right before click_on I see the following code:
<html xmlns=\\\"http://www.w3.org/1999/xhtml\\\"><head></head><body></body></html>\
But if I change that for p response.body.inspect then I get the full html code of the page..., shouldn't the visit method load the content of the response on the page?
Any help will be appreciated, this is driving me crazy :)
Add that line:
config.include Capybara::DSL
to spec/spec_helper.rb:
RSpec.configure do |config|
.....
config.include Capybara::DSL
.....
end