Because you're to cool not to fork the release candidate

Build nested field only for new entry

I have a nested form in which I would like the form fields to only appear once for uploading a new image, then display the existing images, without any sort of CRUD capability. In essence, on the edit page, just give the option to add images, but not delete or update.

The relationship is a video has many video_images, which use paperclip for has_attached_file :image I’m trying the following in my controller now:

  # GET /videos/new
 
# GET /videos/new.json
 
def new
   
@video = current_user.videos.build
   
@newthumbnail = @video.video_images.build

    respond_to
do |format|
      format
.html # new.html.erb
      format
.json { render json: @video }
   
end
 
end

And this in my view:

    <%= f.fields_for @newthumbnail do |u| %>
       
<%= u.label :image, "Upload New Thumbnail" %> <br />
       
<%= u.file_field :image, :class => "image_uploader" %>
   
<% end %>
   
<p>
     
Or select a previous thumbnail
   
</p>
    <div id="previous_thumbnails">
      <!-- These will ultimately be dynamically generated as the existing image associations -->
      <div class="previous_thumbnail" rel="1">
        <img src="http:/
/placehold.it/120x90" />
      </div>
      <div class="
selected previous_thumbnail" rel="2">
        <img src="
http://placehold.it/120x90" />
      </div>
      <div class="
previous_thumbnail" rel="3">
        <img src="
http://placehold.it/120x90" />
      </div>
      <div class="
previous_thumbnail" rel="4">
        <img src="
http://placehold.it/120x90" />
      </div>
    </div>

But I end up with the following error:

unknown attribute: video_image

Which tells me that somewhere, there’s a mistranslation of singularity vs plurality. I can see why Rails would singularize it, as it is, in fact, a single entry. However, even overriding the name of the file field, and making name="video[video_image][image]" into name="video[video_images][image]", I get an error like so:

VideoImage(#39741260) expected, got Array(#9591480)

With params sent:

"video_images"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x000000054f1078 @original_filename="Bill OneManBand.jpg",
 
@content_type="image/jpeg",
 
@headers="Content-Disposition: form-data; name=\"video[video_images][image]\"; filename=\"Bill OneManBand.jpg\"\r\nContent-Type: image/jpeg\r\n",
 
@tempfile=#<File:/tmp/RackMultipart20120205-3582-1xp2duq>>},

So where am I going wrong? I feel like I may be taking the wrong approach to this…

Source: stackoverflow.com

Prevent scrolling from anchor tags

I’m making a simple navigation with jQuery that loads content panes by showing or hiding them on the page. I am using anchor tags to determine which pane to show. I also use a “ready” function to show the#hash element when the page is loaded. Pretty basic:

$(document).ready(function() {
  $
('#video_form_content > div').hide();
 
var showTab = window.location.hash || "#basic-info";
  $
(showTab).show();
}

$
('#video_form_tabs li').on('click', function() {
    $
('#video_form_content > div').hide();
   
var target = $(this).children('a').attr('href');
    $
(target).show();
})

What’s happening, though, is that when the elements are clicked, the page jumps to the top of the targetdiv. I don’t want this jump, but I do want to keep the default functionality of the anchor tag in the sense that I want the hash added to the URL so that refreshing would reopen the same tab.

Here’s what I’ve tried

  • Adding $(window).scrollTop(0) to the click event. I thought it might, but this event fires before the actual navigation of the anchor element, so it’s immediately undone.
  • Adding $(window).scrollTop(0) to $(document).ready() However, this listener is not fired when clicking an anchor for the same page.
  • Adding window.location.hash = target or window.location.href = target to the click event. However, this still causes the resubmission of the page, and it still “jumps”.
  • Adding return false to the function. This prevents adding the hash to the URL, which is highly preferred.

This is really baffling me, and any help would be much appreciated.

Source: stackoverflow.com

Just a short meeting with the boss. We’ve all been here before…

This will save me so much money on karate lessons for my kids

This will save me so much money on karate lessons for my kids

Source: themindfulrevolution.com

Regular Expression to Match and Highlight With a Lot of Variance

I’ve taken a few swings at this, but I’ll post my issue and my most recent successful code.

If you’re familiar with Sublime Text, this will make sense much more quickly. If not, I’ll do my best to explain, but I definitely recommend the program as a side note.

The goal is to match titles given a search string and highlight the match. The catch is that the match can be in any order, and doesn’t have to signify the beginning of the string, and that strings are separated by spaces in the original input

Example:

String to Match: "Hello World"

Does it match?
"hello" -> Yes!
"h w" -> Yes!
"el orl" -> Yes!
"World Hello" -> Yes!
"Hello Foo" -> No!

So the input is very forgiving. I have it to the point where I can match the text and determine whether or not to show the result. I’m just stuck at getting highlighting to work. I may be taking the wrong approach, but here is what I have:

$('#sdfilter').live('keyup change', function() {
   
var inText = $(this).val().trim(); //remove trailing whitespace
   
var blocks = $('.listing'); //blocks to loop through

    $
.each(blocks, function() {
       
var title = $(this).children('.title').text(); //the title in the block
       
if (matchAll(title, inText)) {
            $
(this).show();
       
} else {
            $
(this).hide();
       
}
   
})

})

matchAll
= function(string, args) {
   
var die = 0; //return switch
   
var checks = args.split(' '); //break input into array
    $
.each(checks, function() {
       
var myReg = new RegExp(this, 'i'); //search term to regex
       
if (!string.match(myReg)) { //if it doesn't match, kill the function
           
die = 1;
       
}
   
})

   
if (die == 1) {
       
return false;
   
} else {
       
return true;
   
}
}

I’ve tried doing a replace withing the $.each(checks, function() {...}) loop to add <span>tags, but then the input string matches the span tags themselves as it loops around.

I’ve found solutions that are very close, but not quite as dynamic as I’m hoping for. So my official question is: Am I moving in the right direction? And if so, what am I missing here?

Source: stackoverflow.com

Introducing my kid brother to a timeless classic: Counter Strike: Source

Introducing my kid brother to a timeless classic: Counter Strike: Source

jQuery API .data(key, value)

This saved my life today, so I figured it was worth giving credit to.

Source: api.jquery.com